« 类别 Technology 下的文章

Read xml from Resouces folder, and convert to array/dictionary

Create a XML file with name “DB.xml”, and put into “Resources” folder.

<root>
	<item>
		<id>1</id>
		<period>0,2</period>
		<name>car1</name>
		<speed>120km/h</speed>
	</item>
	<item>
		<id>2</id>
		<period>0,1</period>
		<name>car2</name>
		<speed>122km/h</speed>
	</item>
	<item>
		<id>3</id>
		<period>3</period>
		<name>car3</name>
		<speed>123km/h</speed>
	</item>
	<item>
		<id>4</id>
		<period>2,4</period>
		<name>car4</name>
		<speed>110km/h</speed>
	</item>
	<item>
		<id>5</id>
		<period>1,2,3</period>
		<name>car5</name>
		<speed>140km/h</speed>
	</item>
	<item>
		<id>6</id>
		<period>0,4</period>
		<name>car6</name>
		<speed>130km/h</speed>
	</item>
</root>

Switch to Unity

using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
 
Void Start(){
  TextAsset myXML = (TextAsset) Resources.Load("DB");
  var dbArr = XDocument.Load(new System.IO.StringReader(myXML.text)).Root.Elements().
Select(y => y.Elements().ToDictionary(x => x.Name, x => x.Value)).ToArray();
  Debug.Log("try to get first item's name:"+dbArr[0]["name"]);
}

NGUI UILabel encoding example

[00FF00]改變色碼[-]
[b]粗體[/b][i]斜體[/i][u]下底線[/u][s]刪除線[/s]
[sub]下標[/sub][sup]上標[/sup][00FF00][b][i][u]綜合[/b][/i][/u][-]
[url=http://www.google.com/]超連結必須搭配及Box Collider[/url]

Spin like “Google Earth” in Unity

It’s complicated than I thought…

#pragma strict
 
var numberAverages : int = 3;
private var originalRotation : Quaternion;
private var offsetRotation : Quaternion;
 
// Make sure there are always SphereCollider and Rigidbody
@script RequireComponent (SphereCollider)
@script RequireComponent (Rigidbody)
 
function Awake () {
    numberAverages = Mathf.Clamp (numberAverages, 1, numberAverages);
}
 
function OnMouseDown () {
    var hit : RaycastHit;
    var dir : Vector3;
 
    // Stop spinning
    rigidbody.angularVelocity = Vector3.zero;
 
    // Record initial variables 
    if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
        originalRotation = transform.rotation;
        dir = hit.point - transform.position;
        offsetRotation = Quaternion.Inverse (Quaternion.LookRotation (dir));
        Spin (dir);
    }
}
 
function Spin (dir : Vector3) {
    var hit : RaycastHit;
    var privDirList : Array = new Array ();
    var currentDir : Vector3;
 
    // Initialize previous direction list
    for (var i : int = 0; i < numberAverages; i++) {
        privDirList.Add (currentDir);
    }
 
    currentDir = dir;
 
    // Make the object rotate with the cursor while we are grabbing it
    while (Input.GetButton ("Fire1") && Physics.Raycast (camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
        // Remove first element of the array
        privDirList.RemoveAt (0);
 
        // Add current direction to the end
        privDirList.Add (currentDir);
        currentDir = hit.point - transform.position;
        transform.rotation =  Quaternion.LookRotation (currentDir) * offsetRotation * originalRotation;
        yield;
    }
 
    // User let go of the mouse so make the object spin on its own
    var avgPrevDir : Vector3 = Vector3.zero;
    var newPDirList : Vector3[] = privDirList.ToBuiltin(Vector3);
    for (dir in newPDirList) {
        avgPrevDir += dir;
    }
 
    // Keep spinning with the direction when you end touching
    avgPrevDir /= numberAverages;
    MoveAcceleration (currentDir, avgPrevDir); 
}
 
function MoveAcceleration (r2 : Vector3, r1 : Vector3) {
    var linearVelocity : Vector3;
    var angVelocity : Vector3;
 
    // Calculate the angular velocity:  Omega = r x v / r^2
    linearVelocity = (r2 - r1) / Time.deltaTime;
    rigidbody.angularVelocity = Vector3.Cross (r2, linearVelocity) / r2.sqrMagnitude;
}

how to create instance in Unity

#1 Create Class Car.

using UnityEngine;
using System.Collections;
 
public class Car : ScriptableObject {
	public string color;
 
	void Awake(){
		color = "red";
	}
}

#2 Create instance.

using UnityEngine;
using System.Collections;
 
public class main : MonoBehaviour {
	void Start(){
		Car polo = (Car) ScriptableObject.CreateInstance(typeof(Car));
	}
}

Json Parser

A very useful tools for checking Json format error.
http://json.parser.online.fr/

[转]C# 中 List 用法

List 类是 ArrayList 类的泛型等效类,某些情况下,用它比用数组和 ArrayList 都方便。
我们假设有一组数据,其中每一项数据都是一个结构。

public struct Item
{
    public int Id;
    public string DisplayText;
}

注意结构是不能给实例字段赋值的,即 public int Id = 1 是错误的。

using System.Collections.Generic;
 
List<Item> items = new List<Item>();
 
//添加
Item item1 = new Item();
item1.Id = 0;
item1.DisplayText = "水星";
items.Add(item1);
 
//添加
Item item2 = new Item();
item2.Id = 1;
item2.DisplayText = "地球";
items.Add(item2);
 
//修改
//这里使用的是结构,故不能直接用 items[1].DisplayText = "金星";
//如果 Item 是类,则可以直接用。为什么呢?因为结构是按值传递的。
Item item = items[1];
item.DisplayText = "金星";
items[1] = item;