1. 程式人生 > >4.unity3D 預設的一例

4.unity3D 預設的一例

des tco events component engine ica frame initial identity

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 () {

}
}

4.unity3D 預設的一例