Exemplo n.º 1
0
        public static object GetComponentVar(Type t1, Type t2, object v)
        {
            v = GameMain.Instance.MainCamera.gameObject.GetComponent(t1);
            if (v == null)
            {
                v = GameMain.Instance.MainCamera.gameObject.AddComponent(t1);

                string[] sShaders = Util.GetFieldNamesSpecifyType(t1, typeof(Shader));
                foreach (string sShader in sShaders)
                {
                    Shader shader = assetShaders.LoadAsset(sShader) as Shader;
                    if (shader == null)
                    {
                        if (sShader == "blurShader")
                        {
                            shader = assetShaders.LoadAsset("separableBlurShader") as Shader;
                        }
                        else
                        {
                            shader = assetShaders.LoadAsset(t1.Name + "_" + sShader) as Shader;
                        }
                        if (shader == null)
                        {
                            shader = extraShaders.LoadAsset(t1.Name + "_" + sShader) as Shader;
                        }

                        if (shader == null)
                        {
                            Debug.LogError("Shader " + t1.Name + " has no shader file in extrashaders.");
                            continue;
                        }
                    }

                    Util.SetFieldValue(t1, v, sShader, shader);
                }

                MethodInfo mi = t2.GetMethod("InitExtra");
                if (mi != null)
                {
                    mi.Invoke(null, new object[] { v });
                }
            }
            else
            {
                Util.SetPropertyValue(t1, v, "enabled", true);

                MethodInfo mi = t2.GetMethod("InitMemberByInstance");
                if (mi != null)
                {
                    mi.Invoke(null, new object[] { v });
                }
            }

            return(v);
        }
Exemplo n.º 2
0
        public static T GetComponentVar <T, T2>(T v) where T : UnityEngine.Component where T2 : class
        {
            if (v != null)
            {
                return(v);
            }

            v = GameMain.Instance.MainCamera.gameObject.GetComponent <T>();
            if (v == null)
            {
                v = GameMain.Instance.MainCamera.gameObject.AddComponent <T>();

                string[] sShaders = Util.GetFieldNamesSpecifyType <T, Shader>();

                //Type typeShaders = typeof(Shaders);
                //foreach (string sShader in sShaders)
                //{
                //    FieldInfo field = typeShaders.GetField(sShader);
                //    if (field == null)
                //    {
                //        if (sShader == "blurShader")
                //        {
                //            field = typeShaders.GetField("separableBlurShader");
                //        }
                //        else
                //        {
                //            field = typeShaders.GetField(typeof(T).Name + "_" + sShader);
                //        }
                //        if (field == null)
                //            continue;
                //    }

                //    Shader shader = new Material((string)field.GetValue(null)).shader;
                //    Util.SetFieldValue<T>(v, sShader, shader);
                //}

                foreach (string sShader in sShaders)
                {
                    Shader shader = assetShaders.LoadAsset(sShader) as Shader;
                    if (shader == null)
                    {
                        if (sShader == "blurShader")
                        {
                            shader = assetShaders.LoadAsset("separableBlurShader") as Shader;
                        }
                        else
                        {
                            shader = assetShaders.LoadAsset(typeof(T).Name + "_" + sShader) as Shader;
                        }

                        if (shader == null)
                        {
                            shader = extraShaders.LoadAsset(typeof(T).Name + "_" + sShader) as Shader;
                        }

                        if (shader == null)
                        {
                            Debug.LogError("Shader " + typeof(T).Name + " has no shader file in extrashaders.");
                            continue;
                        }
                    }

                    Util.SetFieldValue <T>(v, sShader, shader);
                }


                Type       typeDef = typeof(T2);
                MethodInfo mi      = typeDef.GetMethod("InitExtra");
                if (mi != null)
                {
                    mi.Invoke(null, new object[] { v });
                }
            }
            else
            {
                Util.SetPropertyValue <T>(v, "enabled", true);
                Type       typeDef = typeof(T2);
                MethodInfo mi      = typeDef.GetMethod("InitMemberByInstance");
                if (mi != null)
                {
                    mi.Invoke(null, new object[] { v });
                }
            }

            return(v);
        }