// ReSharper disable MemberCanBeMadeStatic.Global public void Process() // ReSharper restore MemberCanBeMadeStatic.Global { // reflect all types /*var snippet = AllTypesSnipetCreator.Create(); * CreateTypeListExec.Run(snippet);*/// TODO if (!AddEventHandlerPersistedData.PostCompileProcessingMode) { return; } if (AddEventHandlerPersistedData.Saved) { AddEventHandlerPersistedData.Saved = false; //Debug.Log("Loading persisted data..."); // process var persistedData = AddEventHandlerPersistedData.Load(); // Debug.Log(@"persistedData: //" + persistedData); PersistedDataProcessor.Instance.Process(persistedData); } }
public static Component AddHandlerScript(ComponentAdapter adapter, AddEventHandlerPersistedData data) { //Debug.Log(string.Format(@"AddHandlerScript [adapter: {0}, data: {1}]", adapter, data)); var component = adapter.gameObject.GetComponent(data.ClassName); Component addedComponent = null; /** * 1. Check if the component is already attached * */ if (null != component) { string text = string.Format(@"Script ""{0}"" is already attached to the selected game object.", data.ClassName); Debug.LogWarning(text); EditorUtility.DisplayDialog("Duplicated script", text, "OK"); } else { /** * 2. Add component * */ addedComponent = adapter.gameObject.AddComponent(data.ClassName); } return addedComponent; }
private static void CreateMapping(AddEventHandlerPersistedData persistedData, ComponentAdapter adapter) { EventMapping mapping = new EventMapping { EventType = persistedData.EventName, ScriptName = persistedData.ClassName, MethodName = persistedData.MethodName, Phase = (EventPhase)persistedData.EventPhases }; adapter.EventMap.Add(mapping); //EventDisplay.Instance.Refresh(); // TODO: Crashes the app }
// ReSharper disable MemberCanBeMadeStatic.Global public void Process() // ReSharper restore MemberCanBeMadeStatic.Global { if (AddEventHandlerPersistedData.PostCompileProcessingMode) { return; } if (AddEventHandlerPersistedData.Saved) { AddEventHandlerPersistedData.Saved = false; // process var persistedData = AddEventHandlerPersistedData.Load(); PersistedDataProcessor.Instance.Process(persistedData); } }
public static void Run(ComponentAdapter adapter, AddEventHandlerPersistedData persistedData) { //if (EditorApplication.isPlaying) //{ // //Debug.Log("Stopping..."); // EditorApplication.isPlaying = false; //} _adapter = adapter; //Debug.Log("*** persistedData.ScriptPath: " + persistedData.ScriptPath); //Debug.Log("*** persistedData.Snippet: " + persistedData.Snippet); /** * 4. Save file * */ SaveFile(persistedData.ScriptPath, persistedData.Snippet); }
/// <summary> /// 3. Loading data from EditorSettings /// Used if was in play mode and the play mode has been stopped /// </summary> public static AddEventHandlerPersistedData Load() { AddEventHandlerPersistedData persistedData = new AddEventHandlerPersistedData { EventName = EditorPrefs.GetString(EventNameKey), Action = EditorPrefs.GetString(ActionKey), ScriptPath = EditorPrefs.GetString(ScriptPathKey), ClassName = EditorPrefs.GetString(ClassNameKey), MethodName = EditorPrefs.GetString(MethodNameKey), //AttachedScriptType = EditorPrefs.GetString(AttachedScriptTypeKey), Snippet = EditorPrefs.GetString(SnippetKey), SnippetLineNumber = EditorPrefs.GetInt(SnippetLineNumberKey), EventPhases = EditorPrefs.GetInt(EventPhasesKey), OpenScript = EditorPrefs.GetBool(OpenScriptKey), TransformInstanceId = EditorPrefs.GetInt(TransformInstanceIdKey), ScriptAlreadyAttached = EditorPrefs.GetBool(ScriptAlreadyAttachedKey) }; return(persistedData); }
/// <summary> /// 1. Creates the persisted data from ordinary data object /// We are using the persisted data even if we are not in play mode /// This is because we want to have a single pipeline for both cases /// </summary> /// <param name="dataObject"></param> /// <returns></returns> public static AddEventHandlerPersistedData FromDataObject(AddHandlerDataObject dataObject) { AddEventHandlerPersistedData persistedData = new AddEventHandlerPersistedData { EventName = dataObject.GetEventName(), Action = dataObject.Action, ScriptPath = dataObject.ScriptPath, ClassName = dataObject.ClassName, MethodName = dataObject.MethodName, //AttachedScriptType = null == dataObject.AttachedScriptType ? null : dataObject.AttachedScriptType.ToString(), Snippet = dataObject.Snippet, SnippetLineNumber = dataObject.SnippetLineNumber, EventPhases = (int)dataObject.EventPhases, OpenScript = dataObject.OpenScript, TransformInstanceId = dataObject.TransformInstanceId, ScriptAlreadyAttached = dataObject.ScriptAlreadyAttached }; return(persistedData); }
/// <summary> /// Processes the last wizard step /// </summary> internal void Process() { // grab the _data var data = AddEventHandlerDialog.Instance.Data; // method name data.MethodName = _methodName; // should we add the casting line, and do we have enough information to cast? data.Cast = _cast && null != data.EventAttribute && data.EventAttribute.Type.IsSubclassOf(typeof(Core.Events.Event)); // add creation handlers? data.AddComponentInstantiatedHandler = _addComponentInstantiatedHandler; data.AddInitializeComponentHandler = _addInitializeComponentHandler; // 1. set the OpenScript flag data.OpenScript = _openScript; // 2. create snippet switch (data.Action) { case AddHandlerAction.CreateNewScriptAndHandler: SnippetCreator.CreateSnippet(data); break; case AddHandlerAction.CreateNewHandlerInExistingScript: SnippetCreator.InsertHandler(data); break; } // copy event phases data.EventPhases = 0; if (_capturePhase) { data.EventPhases = data.EventPhases | EventPhase.Capture; } if (_targetPhase) { data.EventPhases = data.EventPhases | EventPhase.Target; } if (_bubblingPhase) { data.EventPhases = data.EventPhases | EventPhase.Bubbling; } // save the transform instance ID - it will be looked upon the play mode stop data.TransformInstanceId = AddEventHandlerDialog.Instance.Adapter.transform.GetInstanceID(); // convert to persisted object AddEventHandlerPersistedData persistedData = AddEventHandlerPersistedData.FromDataObject(data); switch (data.Action) { //case AddHandlerAction.MapExistingHandler: //case AddHandlerAction.AttachExistingScriptAndMapHandler: default: AddEventHandlerPersistedData.PostCompileProcessingMode = false; break; case AddHandlerAction.CreateNewHandlerInExistingScript: case AddHandlerAction.CreateNewScriptAndHandler: /** * In the case of editing scripts, the application will be recompiled (automatically) * so we will do the rest after the recompiling process is finished (not after the play mode is stopped) * */ AddEventHandlerPersistedData.PostCompileProcessingMode = true; break; } PersistedDataProcessingLogic.Process(persistedData); }
/// <summary> /// 3. Loading data from EditorSettings /// Used if was in play mode and the play mode has been stopped /// </summary> public static AddEventHandlerPersistedData Load() { AddEventHandlerPersistedData persistedData = new AddEventHandlerPersistedData { EventName = EditorPrefs.GetString(EventNameKey), Action = EditorPrefs.GetString(ActionKey), ScriptPath = EditorPrefs.GetString(ScriptPathKey), ClassName = EditorPrefs.GetString(ClassNameKey), MethodName = EditorPrefs.GetString(MethodNameKey), //AttachedScriptType = EditorPrefs.GetString(AttachedScriptTypeKey), Snippet = EditorPrefs.GetString(SnippetKey), SnippetLineNumber = EditorPrefs.GetInt(SnippetLineNumberKey), EventPhases = EditorPrefs.GetInt(EventPhasesKey), OpenScript = EditorPrefs.GetBool(OpenScriptKey), TransformInstanceId = EditorPrefs.GetInt(TransformInstanceIdKey), ScriptAlreadyAttached = EditorPrefs.GetBool(ScriptAlreadyAttachedKey) }; return persistedData; }
/// <summary> /// 1. Creates the persisted data from ordinary data object /// We are using the persisted data even if we are not in play mode /// This is because we want to have a single pipeline for both cases /// </summary> /// <param name="dataObject"></param> /// <returns></returns> public static AddEventHandlerPersistedData FromDataObject(AddHandlerDataObject dataObject) { AddEventHandlerPersistedData persistedData = new AddEventHandlerPersistedData { EventName = dataObject.GetEventName(), Action = dataObject.Action, ScriptPath = dataObject.ScriptPath, ClassName = dataObject.ClassName, MethodName = dataObject.MethodName, //AttachedScriptType = null == dataObject.AttachedScriptType ? null : dataObject.AttachedScriptType.ToString(), Snippet = dataObject.Snippet, SnippetLineNumber = dataObject.SnippetLineNumber, EventPhases = (int)dataObject.EventPhases, OpenScript = dataObject.OpenScript, TransformInstanceId = dataObject.TransformInstanceId, ScriptAlreadyAttached = dataObject.ScriptAlreadyAttached }; return persistedData; }
// ReSharper disable MemberCanBeMadeStatic.Global public void Process(AddEventHandlerPersistedData persistedData) // ReSharper restore MemberCanBeMadeStatic.Global { //Debug.Log("Process"); int instanceId = persistedData.TransformInstanceId; Transform transform = EditorUtility.InstanceIDToObject(instanceId) as Transform; if (null == transform) { Debug.LogError("Cannot find object with InstanceID=" + instanceId); } else { ComponentAdapter adapter = GuiLookup.GetAdapter(transform); if (null == adapter) { Debug.LogError("Cannot find adapter on transform having InstanceID=" + instanceId); } else { //Debug.Log("Applying persistedData.Action: " + persistedData.Action); Component cmp; switch (persistedData.Action) // MapExistingHandler, AttachExistingScriptAndMapHandler, CreateNewScriptAndHandler { //case AddHandlerAction.MapExistingHandler: default: CreateMapping(persistedData, adapter); EventDisplay.Instance.Refresh(); cmp = adapter.gameObject.GetComponent(persistedData.ClassName); break; case AddHandlerAction.AttachExistingScriptAndMapHandler: /** * In the case of the new handler creation, we are allowing the already attached script to be "attached" again * It is not really attached if it already exists - we just don't throw an exception * So, if the ScriptAlreadyAttached flag is true, we are referencing the script using gameObject.GetComponent, * else we are using IO.Util * */ cmp = persistedData.ScriptAlreadyAttached ? adapter.gameObject.GetComponent(persistedData.ClassName) : IO.Util.AddHandlerScript(adapter, persistedData); if (null == cmp) { Debug.LogError("Couldn't add event handler script"); return; } CreateMapping(persistedData, adapter); //EventDisplay.Instance.Refresh(); break; case AddHandlerAction.CreateNewHandlerInExistingScript: /** * In the case of the new handler creation, we are allowing the already attached script to be "attached" again * It is not really attached if it already exists - we just don't throw an exception * So, if the ScriptAlreadyAttached flag is true, we are referencing the script using gameObject.GetComponent, * else we are using IO.Util * */ cmp = persistedData.ScriptAlreadyAttached ? adapter.gameObject.GetComponent(persistedData.ClassName) : IO.Util.AddHandlerScript(adapter, persistedData); if (null == cmp) { Debug.LogError("Couldn't add event handler script"); return; } CreateMapping(persistedData, adapter); //EventDisplay.Instance.Refresh(); break; case AddHandlerAction.CreateNewScriptAndHandler: cmp = IO.Util.AddHandlerScript(adapter, persistedData); if (null == cmp) { Debug.LogError("Couldn't add event handler script"); return; } CreateMapping(persistedData, adapter); break; } if (null == cmp) // MapExistingHandler { throw new Exception("Couldn't add event the script"); } MonoBehaviour mb = cmp as MonoBehaviour; if (null == mb) { throw new Exception("Component is not a MonoBehaviour"); } if (persistedData.OpenScript) { // open script var script = MonoScript.FromMonoBehaviour(mb); //Debug.Log("persistedData.SnippetLineNumber: " + persistedData.SnippetLineNumber); AssetDatabase.OpenAsset(script, persistedData.SnippetLineNumber); } /** * 3. Re-scan the hierarchy * */ HierarchyViewDecorator.Instance.ReScan(/*adapter.GetInstanceID()*/); } } }
private static void CreateMapping(AddEventHandlerPersistedData persistedData, ComponentAdapter adapter) { EventMapping mapping = new EventMapping { EventType = persistedData.EventName, ScriptName = persistedData.ClassName, MethodName = persistedData.MethodName, Phase = (EventPhase) persistedData.EventPhases }; adapter.EventMap.Add(mapping); //EventDisplay.Instance.Refresh(); // TODO: Crashes the app }
public static void Process(AddEventHandlerPersistedData persistedData) { switch (persistedData.Action) { default: /** * 1. Mapping the existing handler * Play mode could have be stopped - OPTIONAL * */ if (persistedData.OpenScript && EditorApplication.isPlaying) { const string title = "Stop the play mode?"; const string msg = @"You have chosen to open a script after mapping the handler. Do you want to stop the play mode? NOTE: This is recommended if you are planning to edit the script."; if (EditorUtility.DisplayDialog(title, msg, "Yes", "No")) { // opening script. Save and stop the app. //Debug.Log("Stopping the play mode..."); persistedData.Save(); EditorApplication.isPlaying = false; // stop the play mode } else { // run now PersistedDataProcessor.Instance.Process(persistedData); } } else { // run now //Debug.Log("Executing immediatelly"); PersistedDataProcessor.Instance.Process(persistedData); } break; case AddHandlerAction.AttachExistingScriptAndMapHandler: /** * 2. Adding the existing script * Play mode should be stopped - OPTIONAL: if script not already attached * */ if (EditorApplication.isPlaying && !persistedData.ScriptAlreadyAttached) { persistedData.Save(); DisplayStoppingMessage("because a script is being added"); EditorApplication.isPlaying = false; // stop the play mode } else { PersistedDataProcessor.Instance.Process(persistedData); } return; case AddHandlerAction.CreateNewHandlerInExistingScript: /** * 3. Creating a new handler * Play mode should be stopped - MANDATORY * */ CreateNewHandlerScriptFileExec.Run(AddEventHandlerDialog.Instance.Adapter, persistedData); //Debug.Log("CreateNewHandlerInExistingScript!"); if (EditorApplication.isPlaying) { bool mandatoryStop = EditorApplication.isPlaying && !persistedData.ScriptAlreadyAttached; if (mandatoryStop) // script not yet added { persistedData.Save(); DisplayStoppingMessage("because a script is being added"); EditorApplication.isPlaying = false; // stop the play mode } else { const string title = "Stop the play mode?"; const string msg = @"You have chosen to edit a script while in Play mode. Do you want to stop the play mode? NOTE: This is recommended if you are planning to edit the script."; if (EditorUtility.DisplayDialog(title, msg, "Yes", "No")) { persistedData.Save(); EditorApplication.isPlaying = false; // stop the play mode } else { PersistedDataProcessor.Instance.Process(persistedData); } } } else { PersistedDataProcessor.Instance.Process(persistedData); } break; case AddHandlerAction.CreateNewScriptAndHandler: /** * 4. Creating new script * Play mode should be stopped - MANDATORY * */ CreateNewHandlerScriptFileExec.Run(AddEventHandlerDialog.Instance.Adapter, persistedData); persistedData.Save(); if (EditorApplication.isPlaying) { DisplayStoppingMessage("to be recompiled, because a new script is being created"); EditorApplication.isPlaying = false; // stop the play mode } break; } }