4.unity3D 预设的一例

Posted xiajing12345

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4.unity3D 预设的一例相关的知识,希望对你有一定的参考价值。

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 预设的一例的主要内容,如果未能解决你的问题,请参考以下文章

Unity3d学习 预设体(prefab)的一些理解

Unity3D 基于预设(Prefab)的泛型对象池实现

为Unity3D创建素材(3):预设物体(Prefab)

unity4怎么创建一个横向的洞穴,用地形降低的方法不行

记录一下工作中遇到的一例kibana无法启动的问题

VisualSVN的一例图标无法显示解决办法