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

    }
    }

  • 相关阅读:
    9.类与对象二----重载、值传递、可变个数形参
    1.数据库相关概念
    8.类与对象一举例
    7.类与对象一
    Educational Codeforces Round 91 (Rated for Div. 2) B. Universal Solution(思维)
    VS Code配置C++环境: Unable to start debugging. Unexpected GDB output from command报错
    Codeforces Round #655 (Div. 2) C. Omkar and Baseball(思维)
    Redis高可用,高性能,高并发
    .Net Core 中GC的工作原理
    大数据环境下该如何优雅地设计数据分层
  • 原文地址:https://www.cnblogs.com/xiajing12345/p/7211946.html
Copyright © 2020-2023  润新知