NGU-学习笔记(1)-动态添加删除图集

 

现在 正在做unity的方向 不得不说我选的是UI方向 Unity中很有名的就是NGUI插件了.今天做了个ngui的简单背包系统。非常简陋。。初学着 自己mark下

(1)预览

主要就是个 simple popup list控件(为label制作动态字体方便支持中文)

设置如图

 mark为空物体,作为动态生成的图集的坐标。为他绑定脚本 child:

using UnityEngine;
using System.Collections;

public class child : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    
    }
    
    // Update is called once per frame
    void Update () {
        Transform[] chinds = this.GetComponentsInChildren<Transform>();
        if (chinds.Length >= 3)
        { Destroy(chinds[1].gameObject); }
        

    }
}

camera得脚本:

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class load_sprite : MonoBehaviour
 5 {
 6     public GameObject parent;
 7     private UIDragObject ud;
 8     // Use this for initialization
 9     void Start()
10     {
11        
12     }
13 
14     // Update is called once per frame
15     void Update()
16     {
17         parent.transform.position = new Vector3(-1.782f, -0.29f, 0);
18     }
19     public void create()
20     {
21         UIAtlas atlas = (UIAtlas)Resources.Load("Atlas/New Atlas_1", typeof(UIAtlas));
22         UISprite sprite = NGUITools.AddSprite(parent, atlas, UIPopupList.current.value);
23         sprite.transform.localScale = new Vector3(2.0f, 2.0f, 2.0f);
24         sprite.transform.localPosition = Vector3.zero;
25         sprite.gameObject.AddComponent<BoxCollider>().size=new Vector3(100,100,0);
26         sprite.gameObject.AddComponent<Drop_PM>();
27         sprite.tag = "item";
28         sprite.depth = 2;
29 
30 
31         GameObject go = GameObject.Find("Sprite");
32         BoxCollider col = go.AddComponent<BoxCollider>();
33         col.size = new Vector3(100, 100, 0);
34         go.AddComponent<UIDragDropItem>();
35         go.GetComponent<UIWidget>().depth = 10;
36 
37 
38      
39 
40 
41     }
42 }

project视图(必须为Resources,内放图集)

还有个动态加载的脚本 重写uidragdropitem

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class MyDragItem : UIDragDropItem {
 5 
 6     protected override void OnDragDropRelease(GameObject surface)
 7     {
 8         base.OnDragDropRelease(surface);
 9 
10         print(surface);
11 
12         this.transform.parent = surface.transform;
13         this.transform.localPosition = Vector3.zero;
14     }
15 }


很简单的一个入门实验。

 

posted @ 2015-04-14 15:28  mukeyang  阅读(333)  评论(0编辑  收藏  举报