예제 #1
0
        private void Create()
        {
            CreateScript();

            if (CanAddComponent())
            {
                InternalEditorUtility.AddScriptComponentUnchecked(m_GameObjectToAddTo,
                                                                  AssetDatabase.LoadAssetAtPath(TargetPath(), typeof(MonoScript)) as MonoScript);
            }

            Close();
            GUIUtility.ExitGUI();
        }
    private void saveAndAttachScript()
    {
        var scenePath = Path.GetDirectoryName(EditorApplication.currentScene).Replace("\\", "/");
        var path      = EditorUtility.SaveFilePanel("Create Script", scenePath, className, "cs");

        if (string.IsNullOrEmpty(path))
        {
            return;
        }

        var filename = Path.GetFileNameWithoutExtension(path);

        if (!Regex.IsMatch(filename, "^[a-zA-Z$_]+[a-zA-Z0-9$_]*$"))
        {
            EditorUtility.DisplayDialog("Invalid file name", "You have chosen a file name that cannot be used as a valid identifier for a MonoBehavior", "CANCEL");
            return;
        }

        using (var file = File.CreateText(path))
        {
            className = filename;
            file.WriteLine(generateScript());
        }

        AssetDatabase.Refresh();
        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport);

        var gameObj = target.gameObject;
        var script  = AssetDatabase.LoadAssetAtPath(path.MakeRelativePath(), typeof(MonoScript)) as MonoScript;

        InternalEditorUtility.AddScriptComponentUnchecked(gameObj, script);

        #region Delayed execution

        // Declared with null value to eliminate "uninitialized variable"
        // compiler error in lambda below.
        EditorApplication.CallbackFunction callback = null;

        callback = () =>
        {
            AssetDatabase.OpenAsset(script);
            EditorApplication.delayCall -= callback;
        };

        EditorApplication.delayCall += callback;

        #endregion

        Close();
        GUIUtility.ExitGUI();
    }
예제 #3
0
 public void Create()
 {
     if (!this.CanCreate())
     {
         return;
     }
     this.CreateScript();
     GameObject[] gameObjects = AddComponentWindow.gameObjects;
     for (int i = 0; i < gameObjects.Length; i++)
     {
         GameObject gameObject = gameObjects[i];
         MonoScript monoScript = AssetDatabase.LoadAssetAtPath(this.TargetPath(), typeof(MonoScript)) as MonoScript;
         monoScript.SetScriptTypeWasJustCreatedFromComponentMenu();
         InternalEditorUtility.AddScriptComponentUnchecked(gameObject, monoScript);
     }
     AddComponentWindow.s_AddComponentWindow.Close();
 }
예제 #4
0
        public static void CreateNewFileOfType()
        {
            CreateScript();

            CreateAssets();
            AssetDatabase.Refresh();
            InternalEditorUtility.AddScriptComponentUnchecked(_createdViewObject, AssetDatabase.LoadAssetAtPath(TargetPath(), typeof(MonoScript)) as MonoScript);
            if (_createModel)
            {
                InternalEditorUtility.AddScriptComponentUnchecked(_createdModelObject, AssetDatabase.LoadAssetAtPath(TargetModelPath(), typeof(MonoScript)) as MonoScript);
            }
            _createdViewObject.AddComponent <Animation>();
#if UNITY_4_6 || UNITY_5_0
            CreateCanvas(_createdViewObject);
#endif
            if (_window != null)
            {
                _window.Close();
            }
        }