NGUI-学习笔记(2)一个项目需求

                                                                                      using UnityEngine;
using System.Collections;

public class ins1 : MonoBehaviour
{
    
    //bool isTarget = false;
    private Vector3 world;
    private Vector3 screenpos;
    private Vector3 mousepos;
    private Vector3 offset;
    public RaycastHit hit;
    private Vector3 obj_pos;
    private GameObject obj;
    //private int speed;
    //LayerMask mask_3 = 1 << 10;
    //LayerMask mask_2 = 11 << 13;
    // Use this for initialization
    void Start()
    {
        StartCoroutine(move());
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        { 
            Ray();
        }
    }
    IEnumerator move()
    {
        screenpos = Camera.main.WorldToScreenPoint(this.transform.position);

        offset = this.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenpos.z));
        while (true)
        {
            mousepos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenpos.z);
            world = Camera.main.ScreenToWorldPoint(mousepos) + offset;
            this.transform.position = world;
            yield return new WaitForFixedUpdate();
        }
    }
    public void Ray()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        //RaycastHit hit;
        GameObject hitobj = null;
        if (Physics.Raycast(ray, out hit))
        {
            hitobj = hit.collider.gameObject;
            print(hitobj.name);

            if (hit.collider.gameObject.name == "plane")
            {
                obj_pos = hit.point;
                obj = (GameObject)Instantiate(this.gameObject);
                obj.transform.position = obj_pos;
                Destroy(obj.GetComponent<ins1>());
                Destroy(this.gameObject);
            }
        }
        else
            Destroy(this.gameObject); 


    }
   
    
}

 

  

主要实现选择label 显示图片 拖拽物体的功能。

预览:

过程:(1)添加label attach boxcollider UIbutton

添加sprite 如图

还有plane

(2)为label绑定脚本

using UnityEngine;
using System.Collections;


public class getText : MonoBehaviour {
    
    private UILabel label;
    public UIAtlas atlass;
    public UISprite sprite;
    public GameObject[] cells;


	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
    public void get()
    { 
        label=this.GetComponent<UILabel>();
        show(label.text);
     }
    public void show(string name)
    {
        for (int i = 0; i < cells.Length; i++)
        {
            if (cells[i].GetComponent<UISprite>().spriteName.Equals("Right Bracket"))//图集默认图片
           
            {
                sprite = cells[i].GetComponent<UISprite>();
                
                sprite.atlas = atlass;
                sprite.spriteName = name;
                print(name);
                break;
            }
        }


    }
}

  为sprite绑定脚本

using UnityEngine;
using System.Collections;

public class listen : MonoBehaviour
{
    public GameObject[] obj;
    private Vector3 screenpos;
    private Vector3 mousepos;
    private Vector3 world;
    
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    void Awake()
    {
        UIEventListener.Get(this.gameObject).onClick += buttonclick;
    }

    void buttonclick(GameObject button)
    {
        screenpos = Camera.main.WorldToScreenPoint(this.gameObject.transform.position);
        mousepos = Input.mousePosition;
        mousepos.z = screenpos.z;
        world = Camera.main.ScreenToWorldPoint(mousepos);
        string name = button.GetComponent<UISprite>().spriteName;
         for (int i = 0; i < obj.Length; i++)
        {
            if (name == obj[i].name)
            {
               
                GameObject product = (GameObject)Instantiate(obj[i]);
                product.transform.position = world;
                
                this.GetComponent<UISprite>().spriteName = "Right Bracket";
                product.tag = "obj";
                product.layer =10;
                product.AddComponent<ins1>();
            }

        }
    }
}

  ins1脚本:

 

posted @ 2015-04-21 10:05  mukeyang  阅读(199)  评论(0编辑  收藏  举报