如何使用GUITexture拾取对象

时间:2014-02-18 06:33:24

标签: unity3d unityscript

如何实现GUITexture来获取对象?

问题是我有GetButtonUp输入,如何将其更改为GUI Button? use是我的代码类型的图层类型。这是代码:

#pragma strict

var Primary : Item;                     //Your Primary item holder


var Secondary : Item;                   //Your Secondary item holder

var myLayerMask : LayerMask;

private var HitItem : GameObject;       //Item that raycast hits.


function Update ()
{

if(Input.GetButtonUp("Use"))            //Remember to make new Input called "Use" :)
{
var hit: RaycastHit;
var Direction = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, Direction * 2.5, Color.blue);         //Shows raycast      when you press "Use"

    if(Physics.Raycast(transform.position, Direction, hit, 2.5, myLayerMask))
    {
                    if(hit.transform.tag == "Item")         //If raycast hits   transform.tag "Item".
                    {
                            HitItem = hit.transform.gameObject;     //HitItem becomes  hit.gameObject.
                            var ItemScr : ItemScript =  HitItem.GetComponent("ItemScript");

                            if(ItemScr.MyItem.IsSecondary)  //If Hit item is Secondary
                            {
                                    if(Secondary.ActiveObj == null) //Checks if you dont  have Secondary Item
                                    {
                                            Secondary = ItemScr.MyItem;
                                            Destroy(hit.transform.gameObject);
                                            if(Primary.ActiveObj == null)   //Checks if  you have already
                                            {                               //equipped  item in your hand.
                                                    Secondary.ActiveObj.active = true;
                                            }
                                            else if(Primary.ActiveObj)
                                            {
                                                    if(Primary.ActiveObj.active ==  false)
                                                    {
                                                            Secondary.ActiveObj.active = true;
                                                    }
                                            }

                                    }
                            }
                                                                    //And same to primary :)
                            if(!ItemScr.MyItem.IsSecondary) //If Hit item is Primary
                            {
                                    if(Primary.ActiveObj == null)   //If you don't have primary, goes on...
                                    {
                                            Primary = ItemScr.MyItem;
                                            Destroy(hit.transform.gameObject);
                                            if(Secondary.ActiveObj == null)
                                            {
                                                    Primary.ActiveObj.active = true;
                                            }
                                            else if(Secondary.ActiveObj)
                                            {
                                                    if(Secondary.ActiveObj.active ==  false)
                                                    {
                                                            Primary.ActiveObj.active = true;
                                                    }
                                            }
                                    }

                            }
                    }
    }


}

}

1 个答案:

答案 0 :(得分:0)

需要在OnGUI()方法中调用任何GUI调用。通常,GUI脚本与程序的其他部分分开放置,并附加到主摄像机或空对象。 - 我个人使用主相机。 Unity在Here

中走得很远

如果你想改成c#我建议谷歌,我相信这应该是这类问题的完美来源。