Utilities for Unity's built in AssetDatabase class
        /// <summary>
        /// Creates and saves the Scriptable Object as an asset in the Editor. Adapted from Unity3D wiki:
        /// http://wiki.unity3d.com/index.php?title=CreateScriptableObjectAsset
        /// </summary>
        /// <returns>"Reference to the created asset instance</returns>
        /// <typeparam name="T">The type of the Scriptable Object to create.</typeparam>
        public static T CreateAsset <T>() where T : ScriptableObject
        {
            T scriptableObjectInstance = ScriptableObject.CreateInstance <T>();

            string currentPath          = SelectionUtility.GetDirectoryOfSelection();
            string path                 = currentPath;
            string typeName             = typeof(T).ToString();
            string typeWithoutNamespace = typeName.Split('.').Last();
            string uniqueAssetPath      = AssetDatabase.GenerateUniqueAssetPath(path + "New " + typeWithoutNamespace + ".asset");

            AssetDatabaseUtility.SaveAndSelectObject(scriptableObjectInstance, currentPath, Path.GetFileName(uniqueAssetPath));

            return(scriptableObjectInstance);
        }
예제 #2
0
            /// <summary>
            /// Generates a clip from this SpriteAnim
            /// </summary>
            /// <param name="savePath">Save path.</param>
            /// <param name="filenamePrefix">Filename prefix.</param>
            public void GenerateClip(string savePath, string filenamePrefix)
            {
                // Output nothing if there is no clip name
                if (string.IsNullOrEmpty(this.clipName))
                {
                    return;
                }

                // Output nothing if no frames are defined
                if (this.animationKeyframes == null || this.animationKeyframes.Length == 0)
                {
                    return;
                }

                // Get the clip to add our Sprite Animation into, or create a new one.
                AnimationClip builtClip;
                bool          clipIsNew;

                if (this.savedClip == null)
                {
                    builtClip = new AnimationClip();
                    clipIsNew = true;
                }
                else
                {
                    builtClip = this.savedClip;
                    clipIsNew = false;
                }

                builtClip.name      = this.clipName;
                builtClip.frameRate = this.Samples;

                // Set the Looping status of the clip
                AnimationClipSettings clipSettings = new AnimationClipSettings();

                clipSettings.loopTime = this.isLooping;
                AnimationUtility.SetAnimationClipSettings(builtClip, clipSettings);

                // Clear ALL existing sprite bindings in the clip
                EditorCurveBinding[] existingObjectBinding = AnimationUtility.GetObjectReferenceCurveBindings(builtClip);
                for (int i = 0; i < existingObjectBinding.Length; i++)
                {
                    EditorCurveBinding currentBinding = existingObjectBinding[i];
                    if (currentBinding.type == typeof(SpriteRenderer))
                    {
                        AnimationUtility.SetObjectReferenceCurve(builtClip, currentBinding, null);
                    }
                }

                // Clear existing Scale since it will be replaced
                EditorCurveBinding[] existingValueBindings = AnimationUtility.GetCurveBindings(builtClip);
                for (int i = 0; i < existingValueBindings.Length; i++)
                {
                    EditorCurveBinding currentBinding = existingValueBindings[i];
                    if (currentBinding.type == typeof(Transform) && currentBinding.propertyName == "m_LocalScale.x")
                    {
                        builtClip.SetCurve(currentBinding.path, typeof(Transform), "m_LocalScale", null);
                        break;
                    }
                }

                // Initialize the curve property
                EditorCurveBinding curveBinding = new EditorCurveBinding();

                curveBinding.propertyName = "m_Sprite";
                curveBinding.path         = this.PathToSpriteRenderer;
                curveBinding.type         = typeof(SpriteRenderer);

                // Build keyframes for the property
                Sprite[] sprites = AssetDatabaseUtility.LoadSpritesInTextureSorted(this.SourceTexture);
                ObjectReferenceKeyframe[] keys = this.CreateKeysForKeyframeRanges(sprites, this.animationKeyframes, this.Samples);

                // Build the clip if valid
                if (keys != null && keys.Length > 0)
                {
                    // Set the keyframes to the animation
                    AnimationUtility.SetObjectReferenceCurve(builtClip, curveBinding, keys);

                    // Add scaling to mirror sprites
                    // Need to also restore scale in case a clip was previously mirrored and then unflagged
                    AnimationCurve normalCurve = AnimationCurve.Linear(0.0f, 1.0f, builtClip.length, 1.0f);
                    AnimationCurve mirrorCurve = AnimationCurve.Linear(0.0f, -1.0f, builtClip.length, -1.0f);
                    AnimationCurve xCurve      = this.isMirroredX ? mirrorCurve : normalCurve;
                    AnimationCurve yCurve      = this.isMirroredY ? mirrorCurve : normalCurve;
                    builtClip.SetCurve(this.PathToSpriteRenderer, typeof(Transform), "localScale.x", xCurve);
                    builtClip.SetCurve(this.PathToSpriteRenderer, typeof(Transform), "localScale.y", yCurve);
                    builtClip.SetCurve(this.PathToSpriteRenderer, typeof(Transform), "localScale.z", normalCurve);

                    // Create or replace the file
                    string filenameSansExtension = filenamePrefix + "_" + this.clipName;
                    if (clipIsNew)
                    {
                        string filename = filenameSansExtension + ".anim";
                        string fullpath = savePath + filename;
                        AssetDatabase.CreateAsset(builtClip, fullpath);
                    }
                    else
                    {
                        string pathToAsset = AssetDatabase.GetAssetPath(this.savedClip);

                        // renaming file doesn't expect extension for some reason
                        AssetDatabase.RenameAsset(pathToAsset, filenameSansExtension);
                    }

                    // Store reference to created clip to allow overwriting / renaming
                    this.savedClip = builtClip;
                }
                else
                {
                    if (keys == null)
                    {
                        Debug.LogWarning("Skipping clip due to no keys found: " + this.clipName);
                    }
                    else
                    {
                        Debug.LogWarning("Encountered invalid clip. Not enough keys. Skipping clip: " + this.clipName);
                    }
                }
            }