Example #1
0
        static public void FindControls <T>(GameObject obj, ref T controls) where T : new()
        {
            if (controls == null)
            {
                controls = new T();
            }

            var type   = controls.GetType();
            var fields = type.GetFields();

            foreach (var f in fields)
            {
                var attrs = f.GetCustomAttributes(typeof(System.NonSerializedAttribute), false);
                if (attrs.Length != 0)
                {
                    continue;
                }
                f.GetCustomAttributes(typeof(System.NonSerializedAttribute), false);
                var w = GK.FindChild(obj, f.Name, true);

                if (w == null)
                {
                    Debug.LogError("Cannot find widget [" + f.Name + "] in " + GK.Fullname(obj));
                    f.SetValue(controls, null);
                    continue;
                }

                if (f.FieldType == typeof(GameObject))
                {
                    f.SetValue(controls, w);
                }
                else if (f.FieldType.IsSubclassOf(typeof(Component)))
                {
                    var c = w.GetComponent(f.FieldType);
                    f.SetValue(controls, c);
                }
            }
        }