Пример #1
0
        public void Awake()
        {
            if (IsFirst)
            {
                IsFirst = false;
                var prefab = new JPrefab("InstantiateDemo", (result, prefab) =>
                {
                    Log.Print("开始Instantiate");

                    Log.Print("先测试Instantiate (Object original),只有一个参数,为gameObject");
                    GameObject g1 = Object.Instantiate(prefab.Instance);
                    g1.name       = "g1";

                    Log.Print("开始测 Instantiate (Object original, Transform parent),parent是g1");
                    GameObject g2 = Object.Instantiate(prefab.Instance, parent: g1.transform);
                    g2.name       = "g2";

                    Log.Print("开始测有Instantiate (Object original, Transform parent, bool instantiateInWorldSpace),parent是g2,worldPositionStays=true");
                    GameObject g3 = Object.Instantiate(prefab.Instance, parent: g2.transform, true);
                    g3.name       = "g3";

                    Log.Print("开始测有Instantiate (Object original, Vector3 position, Quaternion rotation),position是g3.transform.position,rotation是new Quaternion(0,0,0,0)");
                    GameObject g4 = Object.Instantiate(prefab.Instance, position: g3.transform.position, new Quaternion(0, 0, 0, 0));
                    g4.name       = "g4";

                    Log.Print("开始测有Instantiate (Object original, Vector3 position, Quaternion rotation, Transform parent),position是g4.transform.position,rotation是new Quaternion(0,0,0,0), parent是g2");
                    GameObject g5 = Object.Instantiate(prefab.Instance, position: g4.transform.position, rotation: new Quaternion(0, 0, 0, 0), parent: g2.transform);
                    g5.name       = "g5";

                    Log.Print("开始测试复制物体g1,命名为c1");
                    GameObject c1 = Object.Instantiate(g1);
                    c1.name       = "c1";

                    Log.Print("创建了新的GameObject,t1,挂了一个Canvas组件");
                    GameObject t1 = new GameObject("t1");
                    var canvas    = t1.AddComponent <Canvas>();

                    Log.Print("现在测试复制本地工程的Component,复制的物体为t1,命名为t2");
                    var t2             = Object.Instantiate(canvas);
                    t2.gameObject.name = "t2";
                    Log.Print("t2.name = " + t2);

                    Log.Print("现在测试赋值热更工程的脚本,复制的物体为g4,命名为g6");
                    var g6             = Object.Instantiate(g4.GetComponent <InstantiateDemo>());
                    g6.gameObject.name = "g6";
                    Log.Print("g6.name = " + g6.name + $"({this.GetType().FullName})");

                    Log.Print("需要注意的是,会有一个警告:" +
                              "You are trying to create a MonoBehaviour using the 'new' keyword.  " +
                              "This is not allowed.  MonoBehaviours can only be added using AddComponent(). " +
                              "Alternatively, your script can inherit from ScriptableObject or no base class at all," +
                              "这个暂时没办法解决,忽略即可,无影响");
                });
                return;
            }
        }
Пример #2
0
 public static void InitUIRoot()
 {
     if (_instance != null)
     {
         Log.PrintError("UI Root已存在");
         return;
     }
     JPrefab UIRootPrefab = new JPrefab("UI Root.prefab",
                                        (success, prefab) =>
     {
         if (!success)
         {
             Log.PrintError("UI Root预制体加载失败");
             return;
         }
         GameObject root = prefab.Instantiate("UI Root");
         JBehaviour.CreateOn <UIRootView>(root);
     });
 }