public void SetPropertyValue(object inObj, string fieldName, object newValue) { System.Type t = inObj.GetType(); //GameObject hack // Due to GameObject.active is obsolete and ativeSelf is read only // Use a hack function to override GameObject's active status. if (t == typeof(GameObject) && fieldName == "m_IsActive") { ((GameObject)inObj).SetActive((bool)newValue); return; } // Try search Field first than try property System.Reflection.FieldInfo fieldInfo = t.GetField(fieldName, bindingFlags); if (fieldInfo != null) { fieldInfo.SetValue(inObj, newValue); return; } if (t.ToString().Contains("UnityEngine.")) { fieldName = ViewSystemUtilitys.ParseUnityEngineProperty(fieldName); } System.Reflection.PropertyInfo info = t.GetProperty(fieldName, bindingFlags); if (info != null) { info.SetValue(inObj, newValue); } }
private object GetPropertyValue(object inObj, string fieldName) { System.Type t = inObj.GetType(); //GameObject hack // Due to GameObject.active is obsolete and ativeSelf is read only // Use a hack function to override GameObject's active status. if (t == typeof(GameObject) && fieldName == "m_IsActive") { return(((GameObject)inObj).activeSelf); } object ret = null; // Try search Field first than try property System.Reflection.FieldInfo fieldInfo = t.GetField(fieldName, bindingFlags); if (fieldInfo != null) { ret = fieldInfo.GetValue(inObj); return(ret); } if (t.ToString().Contains("UnityEngine.")) { fieldName = ViewSystemUtilitys.ParseUnityEngineProperty(fieldName); } System.Reflection.PropertyInfo info = t.GetProperty(fieldName, bindingFlags); if (info != null) { ret = info.GetValue(inObj); } //ViewSystemLog.Log($"GetProperty on [{gameObject.name}] Target Object {((UnityEngine.Object)inObj).name} [{t.ToString()}] on [{fieldName}] Value [{ret}]"); return(ret); }
internal void SetEvent(IEnumerable <ViewElementEventData> eventDatas) { currentEventDatas = eventDatas.ToArray(); //Group by Component transform_component_property var groupedEventData = eventDatas.GroupBy(item => item.targetTransformPath + ";" + item.targetComponentType + ";" + item.targetPropertyName); foreach (var item in groupedEventData) { string[] p = item.Key.Split(';'); //p[0] is targetTransformPath Transform targetTansform = GetTransform(p[0]); if (targetTansform == null) { ViewSystemLog.LogError($"Target GameObject cannot be found [{transform.name} / {p[0]}]"); continue; } EventRuntimeDatas eventRuntimeDatas; // Get UnityEvent property instance if (!cachedUnityEvent.TryGetValue(item.Key, out eventRuntimeDatas)) { //p[1] is targetComponentType var result = GetCachedComponent(targetTansform, p[0], p[1]); //p[2] is targetPropertyPath string property = p[2]; if (p[1].Contains("UnityEngine.")) { property = ViewSystemUtilitys.ParseUnityEngineProperty(p[2]); } var unityEvent = (UnityEventBase)GetPropertyValue(result.Component, property); eventRuntimeDatas = new EventRuntimeDatas(unityEvent, (Component)result.Component); cachedUnityEvent.Add(item.Key, eventRuntimeDatas); if (eventRuntimeDatas.unityEvent is UnityEvent events) { events.AddListener(EventHandler); } } currentComponent = eventRuntimeDatas.component; // Usually there is only one event on one Selectable // But the system allow mutil event on one Selectable foreach (var item2 in item) { var id_delegate = item2.scriptName + ";" + item2.methodName; //Try to get the cached openDelegate object first //Or create a new openDelegate if (!cachedDelegate.TryGetValue(id_delegate, out EventDelegate <Component> openDelegate)) { // Get Method Type type = Utility.GetType(item2.scriptName); //MethodInfo method = type.GetMethod(item2.methodName); //The method impletmented Object Component scriptInstance = (Component)FindObjectOfType(type); if (scriptInstance == null) { scriptInstance = GenerateScriptInstance(type); } //Create Open Delegate try { openDelegate = CreateOpenDelegate(item2.methodName, scriptInstance); } catch (Exception ex) { ViewSystemLog.LogError($"Create event delegate faild, make sure the method or the instance is exinst. Exception:{ex.ToString()}", this); } cachedDelegate.Add(id_delegate, openDelegate); } currentEventDelegates.Add(openDelegate); } } }
private void CacheComponent() { if (m_ComponentTreeViewState == null) { m_ComponentTreeViewState = new TreeViewState(); } componentTreeView = new ComponentTreeView( currentSelectGameObject, target, m_ComponentTreeViewState, currentSelectGameObject == target, (a, b) => { return(false); }, () => { }); componentTreeView.OnItemClick += (sp) => { Component c; try { c = (Component)sp.serializedObject.targetObject; } catch { c = ((GameObject)sp.serializedObject.targetObject).transform; } if (sp.propertyType == SerializedPropertyType.Generic) { System.Type parentType = sp.serializedObject.targetObject.GetType(); System.Reflection.FieldInfo fi = parentType.GetField(sp.propertyPath); System.Type propertyType = null; if (fi != null) { propertyType = fi.FieldType; } string propertyName = sp.propertyPath; if (parentType.ToString().Contains("UnityEngine.")) { if (propertyName == "m_Navigation") { var content = new GUIContent("UnityEngine.UI.Navigation is not supported with ViewSystem OverrideSystem"); #if UNITY_2019_1_OR_NEWER editorWindow.ShowNotification(content, toastMessageFadeOutTimt); #else editorWindow.ShowNotification(content); #endif return; } propertyName = ViewSystemUtilitys.ParseUnityEngineProperty(sp.propertyPath); } System.Reflection.PropertyInfo pi = parentType.GetProperty(propertyName); if (pi != null && fi == null) { propertyType = pi.PropertyType; } } else { data.targetTransformPath = AnimationUtility.CalculateTransformPath(c.transform, target.transform); data.targetPropertyName = sp.name; data.targetComponentType = sp.serializedObject.targetObject.GetType().ToString(); data.Value.SetValue(sp); editorWindow.Close(); } sp.serializedObject.ApplyModifiedProperties(); EditorUtility.SetDirty(currentSelectGameObject); }; }