NGUI UILabel encoding example

[url=]超連結必須搭配及Box Collider[/url]

[z] Create Your Own iOS Framework

If you’re new to programming in iOS and are coming from a .Net background (like myself), you may be wondering where all the dlls are.

Well, instead of dlls the platform works in terms Frameworks, which are basically folders thats contain public header files and a binary. Unfortunately, XCode doesn’t natively support the creation of your own Frameworks.

As a control developer, this is kind of a big deal, b/c without frameworks, we have to fall back on creating a static library file. The problem with lib files is they’re a pain to consume. Essentially you add the lib as a reference to your project, and then you have to tell the project where to look for the header files. And thats not a good User Experience.

So how do we get around this limitation?

After doing lots of searching, i stumbled across an awesome open source project on gitHub: iOS-Universal-Framework. The project is basically a project template. You simply install it and now have a new option when creating a project.

So lets take a look at the install process and how to use it:

1. Download the zip file.

2. Make sure you save the file somewhere for future use. B/c its a project template, anytime you update XCode, you’ll have to reinstall the project template. (Don’t worry the install process is really fast)

3. Make sure XCode is completely shut down.

4. Unzip the zip file and navigate to: Real Framework/

5. If you’re familiar with using Terminal, simply call bash on If you’re not familiar, you can simply rename the file to and just double click on it

6. Once it starts running it’s going to ask you: Where is XCode installed. If you haven’t touched the default install location of XCode (in Applications) just hit enter

7. Next it will ask to confirm where its going to install. Just type y and hit enter

8. It will ask you for your password, type it in and hit enter

9. After that you should see Process Complete.

10. Now open up XCode and open the New Project window

11. Under iOS select Framework & Library and you’ll see a new option called “Static iOS Framework”

12. Hit Next and name your framework and follow the normal steps for creating a new project.

So, now you have a new framework! But how do you use it?

You’ll basically treat it like any normal project. However, for any API’s you want made public you need to tell it.

Lets walk through an step by step example of a creating a Framework, exposing your public API’s and consuming it!

1. Create your framework using the new project dialog:

2. You’ll have an empty project now. So lets add a new class. In this case lets create a new UIView and call it MyView

3. Now, since I want these API’s to be available to consumers of this framework, i need to mark the “.h” file as being public. To do that click on the project in the project explorer and select build phases:

If you expand the Copy Headers section, you’ll see that there are 3 options, Public, Private and Project. By default, whenever you add a new file, it will show up under project. If you want to expose it as public, simply drag the header file from project and place it in public:

4. So now lets just a little bit of code to our class so that it does something.

@implementation MyView

self = [super init];
self.backgroundColor = [UIColor orangeColor];
return self;


We’ll simply make our View have a orange background color by default.

5. Now this next step is optional, but in my opinion is good practice. If you’ve ever noticed when you use Apple’s default frameworks, the import statement generally looks likes: #import UIKit/UIKit/.h  or #import  QuartzCore/QuartzCore.h  The nice thing about this, is if you know the name of the framework, then you know which header file to reference. And these header files expose ALL the public API’s in framework. So you don’t have to add a bunch of header files.

So lets do this now.

First open the Add New File dialog and add a new Header file:

Name the header file the exact same name you named your framework. And inside the header file, add a #import for every public header file in your framework.


6. Now Build your project, and LETS USE IT!!!

7. Create a new project, for this case we’re just going to create a Single View Application called MyApp.

8. Once the project its created click on the project and scroll down to “Linked Frameworks and Libraries Section” and click the little plus button:

Now navigate to your framework by clicking the “Add Other” button:

Time to write some code!

9. First open up the ViewController.m file and write your code:

10. Run your project and you should see the following:

And there you go! You’ve written a framework which you can now share between multiple projects and teams!

As always i hope this was helpful.

For more tips ans trick, check out my blog.

By Stephen Zaharuk (SteveZ)

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 =;
    // 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;
    // User let go of the mouse so make the object spin on its own
    var avgPrevDir : Vector3 =;
    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.

[转]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 = "水星";
Item item2 = new Item();
item2.Id = 1;
item2.DisplayText = "地球";
//这里使用的是结构,故不能直接用 items[1].DisplayText = "金星";
//如果 Item 是类,则可以直接用。为什么呢?因为结构是按值传递的。
Item item = items[1];
item.DisplayText = "金星";
items[1] = item;