void OnSceneGUI() { USApplyForceEvent forceEvent = target as USApplyForceEvent; if (!forceEvent) { Debug.LogWarning("Trying to render an event as a USApplyForceEvent, but it is a : " + forceEvent.GetType().ToString()); } if (forceEvent.AffectedObject) { forceEvent.transform.position = forceEvent.AffectedObject.transform.position; } USUndoManager.BeginChangeCheck(); Vector3 vPosition = forceEvent.transform.position; float width = HandleUtility.GetHandleSize(vPosition) * HandleLength; Vector3 vEnd = vPosition + (forceEvent.direction * width); width = HandleUtility.GetHandleSize(vEnd) * HandleSize; vEnd = Handles.FreeMoveHandle(vEnd, Quaternion.identity, width, Vector3.zero, Handles.CubeCap); Vector3 vDifference = vEnd - vPosition; vDifference.Normalize(); // Undo this if (USUndoManager.EndChangeCheck()) { USUndoManager.PropertyChange(forceEvent, "Change Force Event Direction"); forceEvent.direction = vDifference; } Handles.color = Color.red; Handles.DrawLine(vPosition, vEnd); if (GUI.changed) { EditorUtility.SetDirty(forceEvent); } }
public override void OnInspectorGUI() { USUndoManager.BeginChangeCheck(); serializedObject.Update(); USHOTweenEventBase baseTweenEvent = serializedObject.targetObject as USHOTweenEventBase; if (!baseTweenEvent.AffectedObject) { return; } baseTweenEvent.Duration = EditorGUILayout.FloatField("Duration", baseTweenEvent.Duration); if (baseTweenEvent.Duration < 0.0f) { baseTweenEvent.Duration = 1.0f; } baseTweenEvent.Duration = Mathf.Clamp(baseTweenEvent.Duration, 0, float.MaxValue); baseTweenEvent.easeType = (EaseType)EditorGUILayout.EnumPopup("Ease Type", baseTweenEvent.easeType); string[] componentNames = baseTweenEvent.AffectedObject.GetComponents <Component>().Select(c => c.GetType().Name).ToArray(); int componentIndex = baseTweenEvent.TargetComponent != null?componentNames.ToList().FindIndex(n => n == baseTweenEvent.TargetComponent.GetType().Name) : 0; if (componentIndex == -1) { componentIndex = 0; } componentIndex = EditorGUILayout.Popup(componentIndex, componentNames); baseTweenEvent.TargetComponent = baseTweenEvent.AffectedObject.GetComponent(componentNames[componentIndex]); Component selectedComponent = baseTweenEvent.TargetComponent; PropertyInfo[] properties = selectedComponent.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); FieldInfo[] fields = selectedComponent.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); string[] propertyNames = properties.Where(p => IsValidType(p.PropertyType)).Select(p => p.Name).ToArray(); string[] fieldNames = fields.Where(f => IsValidType(f.FieldType)).Select(f => f.Name).ToArray(); string[] allNames = propertyNames.Concat(fieldNames).ToArray(); int propertyIndex = allNames.ToList().FindIndex(n => n == baseTweenEvent.fieldName); if (propertyIndex == -1) { propertyIndex = 0; } propertyIndex = EditorGUILayout.Popup(propertyIndex, allNames); baseTweenEvent.fieldName = allNames[propertyIndex]; PropertyInfo property = properties.Count() > 0 ? properties.Single(p => p.Name == baseTweenEvent.fieldName) : null; FieldInfo field = fields.Count() > 0 ? fields.Single(f => f.Name == baseTweenEvent.fieldName) : null; Type propertyType = null; if (property != null) { propertyType = property.PropertyType; } if (field != null) { propertyType = field.FieldType; } if (propertyType == null) { return; } baseTweenEvent.TargetType = propertyType; DisplayCorrectGUIElement(baseTweenEvent); if (USUndoManager.EndChangeCheck()) { USUndoManager.PropertyChange(serializedObject.targetObject, "Inspector"); USWindow[] windows = Resources.FindObjectsOfTypeAll <USWindow>(); foreach (var window in windows) { window.ExternalModification(); } } serializedObject.ApplyModifiedProperties(); }