4.unity3D 预设的一例

让控件不显示的方法如下:

GameObject btnObj = GameObject.Find("Buttonx"); 

btnObj.setActive("false");

这样 Unity上的控件 就不会显示在界面上了.

下面是通过预设的方式.来处理.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine.UI;

public class TestClick : MonoBehaviour {

public static GameObject my;
// Use this for initialization
void Start () {

// 1.在打开 程序的时候 去查找 这个 button  如果没有找到就新建一个 button
GameObject btnObj = GameObject.Find("Buttonx");
if (btnObj == null) { //如果 btn 没有
my = GameObject.Find ("New Prefab");  // 找到这个 button 的预设
GameObject can = GameObject.Find ("Canvas"); // 找到 canvas 
GameObject go = Instantiate (my, new Vector3 (260, -70, 0), Quaternion.identity); //新建一个 预设 在位置 260,-70
go.name = "Buttonx"; // name 的实例名
go.transform.SetParent (can.transform, false); 把 这个预设加到  Canvas上面.
Debug.Log ("create!");
}
}
public void Click(){
Debug.Log ("Button 134 Clicked. TestClick.");
GameObject btnObj = GameObject.Find("Buttonx");

Button btn = btnObj.GetComponent<Button>();

btn.interactable = false;
// 这里演示怎样删除这个 button ,
GameObject.Destroy (btnObj);
//btn.enabled = false;
GameObject txtObj = GameObject.Find ("Textx");
Text txt = txtObj.GetComponent<Text>();
txt.text="夏静12345";
//Application.Quit();

}
public void Click2(){
GameObject btnObj = GameObject.Find("Buttonx");
if (btnObj == null) {
//GameObject.Instantiate(my,new Vector3(10,10,0),Quaternion.identity);
GameObject can = GameObject.Find ("Canvas");
// 这段代码增加到 can上了,但没有显示
//GameObject buttonadd = new GameObject("buttonadd");
//buttonadd.AddComponent <Button>();
//buttonadd.transform.parent = can.transform;

//
my = GameObject.Find("New Prefab");
//Debug.Log ("abc:"+my.GetType ().ToString ());
//my.
//GameObject buttonPrefab = new GameObject("buttonPrefab"); // 这个是对象的名称
//Text tx =buttonPrefab.AddComponent <Text>();
//tx.font = new Font ("Arial");
//tx.fontStyle = FontStyle.Normal;
//tx.text="add";
//buttonPrefab.transform.SetParent (can.transform,false);


GameObject go = Instantiate (my, new Vector3 (260, -70, 0), Quaternion.identity);
go.name = "Buttonx";
//Debug.Log (go.name);
go.transform.SetParent (can.transform,false);
//这个还不知道是什么意思
//GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//cube.AddComponent<Rigidbody>();
Debug.Log ("btnObj is null");
} else {

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

}
}

posted @ 2017-07-20 16:30  xiajing12345  阅读(153)  评论(0编辑  收藏  举报