testss
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using UnityEditor.Animations;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace AnimationImporter
|
||||
{
|
||||
public static class AnimationClipUtility
|
||||
{
|
||||
class AnimationClipSettings
|
||||
{
|
||||
SerializedProperty m_property;
|
||||
|
||||
private SerializedProperty Get(string property) { return m_property.FindPropertyRelative(property); }
|
||||
|
||||
public AnimationClipSettings(SerializedProperty prop) { m_property = prop; }
|
||||
|
||||
public float startTime { get { return Get("m_StartTime").floatValue; } set { Get("m_StartTime").floatValue = value; } }
|
||||
public float stopTime { get { return Get("m_StopTime").floatValue; } set { Get("m_StopTime").floatValue = value; } }
|
||||
public float orientationOffsetY { get { return Get("m_OrientationOffsetY").floatValue; } set { Get("m_OrientationOffsetY").floatValue = value; } }
|
||||
public float level { get { return Get("m_Level").floatValue; } set { Get("m_Level").floatValue = value; } }
|
||||
public float cycleOffset { get { return Get("m_CycleOffset").floatValue; } set { Get("m_CycleOffset").floatValue = value; } }
|
||||
|
||||
public bool loopTime { get { return Get("m_LoopTime").boolValue; } set { Get("m_LoopTime").boolValue = value; } }
|
||||
public bool loopBlend { get { return Get("m_LoopBlend").boolValue; } set { Get("m_LoopBlend").boolValue = value; } }
|
||||
public bool loopBlendOrientation { get { return Get("m_LoopBlendOrientation").boolValue; } set { Get("m_LoopBlendOrientation").boolValue = value; } }
|
||||
public bool loopBlendPositionY { get { return Get("m_LoopBlendPositionY").boolValue; } set { Get("m_LoopBlendPositionY").boolValue = value; } }
|
||||
public bool loopBlendPositionXZ { get { return Get("m_LoopBlendPositionXZ").boolValue; } set { Get("m_LoopBlendPositionXZ").boolValue = value; } }
|
||||
public bool keepOriginalOrientation { get { return Get("m_KeepOriginalOrientation").boolValue; } set { Get("m_KeepOriginalOrientation").boolValue = value; } }
|
||||
public bool keepOriginalPositionY { get { return Get("m_KeepOriginalPositionY").boolValue; } set { Get("m_KeepOriginalPositionY").boolValue = value; } }
|
||||
public bool keepOriginalPositionXZ { get { return Get("m_KeepOriginalPositionXZ").boolValue; } set { Get("m_KeepOriginalPositionXZ").boolValue = value; } }
|
||||
public bool heightFromFeet { get { return Get("m_HeightFromFeet").boolValue; } set { Get("m_HeightFromFeet").boolValue = value; } }
|
||||
public bool mirror { get { return Get("m_Mirror").boolValue; } set { Get("m_Mirror").boolValue = value; } }
|
||||
}
|
||||
|
||||
public static void SetLoop(this AnimationClip clip, bool value)
|
||||
{
|
||||
SerializedObject serializedClip = new SerializedObject(clip);
|
||||
AnimationClipSettings clipSettings = new AnimationClipSettings(serializedClip.FindProperty("m_AnimationClipSettings"));
|
||||
|
||||
clipSettings.loopTime = value;
|
||||
clipSettings.loopBlend = false;
|
||||
|
||||
serializedClip.ApplyModifiedProperties();
|
||||
}
|
||||
|
||||
// ================================================================================
|
||||
// curve bindings
|
||||
// --------------------------------------------------------------------------------
|
||||
|
||||
public static EditorCurveBinding spriteRendererCurveBinding
|
||||
{
|
||||
get
|
||||
{
|
||||
return new EditorCurveBinding
|
||||
{
|
||||
path = "", // assume SpriteRenderer is at same GameObject as AnimationController
|
||||
type = typeof(SpriteRenderer),
|
||||
propertyName = "m_Sprite"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static EditorCurveBinding imageCurveBinding
|
||||
{
|
||||
get
|
||||
{
|
||||
return new EditorCurveBinding
|
||||
{
|
||||
path = "", // assume Image is at same GameObject as AnimationController
|
||||
type = typeof(Image),
|
||||
propertyName = "m_Sprite"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,156 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using System.IO;
|
||||
|
||||
namespace AnimationImporter
|
||||
{
|
||||
/// <summary>
|
||||
/// Utilities for Unity's built in AssetDatabase class
|
||||
/// </summary>
|
||||
public static class AssetDatabaseUtility
|
||||
{
|
||||
public const char UnityDirectorySeparator = '/';
|
||||
public const string ResourcesFolderName = "Resources";
|
||||
|
||||
public static string projectPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return Application.dataPath.RemoveLastLetters("/Assets".Length);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the asset and any directories that are missing along its path.
|
||||
/// </summary>
|
||||
/// <param name="unityObject">UnityObject to create an asset for.</param>
|
||||
/// <param name="unityFilePath">Unity file path (e.g. "Assets/Resources/MyFile.asset".</param>
|
||||
public static void CreateAssetAndDirectories(UnityEngine.Object unityObject, string unityFilePath)
|
||||
{
|
||||
var pathDirectory = Path.GetDirectoryName(unityFilePath) + UnityDirectorySeparator;
|
||||
|
||||
// necessary fix for Windows because Path.GetDirectoryName is changing the directory separators to Windows style
|
||||
pathDirectory = pathDirectory.Replace('\\', UnityDirectorySeparator);
|
||||
|
||||
CreateDirectoriesInPath(pathDirectory);
|
||||
|
||||
AssetDatabase.CreateAsset(unityObject, unityFilePath);
|
||||
}
|
||||
|
||||
private static void CreateDirectoriesInPath(string unityDirectoryPath)
|
||||
{
|
||||
// Check that last character is a directory separator
|
||||
if (unityDirectoryPath[unityDirectoryPath.Length - 1] != UnityDirectorySeparator)
|
||||
{
|
||||
var warningMessage = string.Format(
|
||||
"Path supplied to CreateDirectoriesInPath that does not include a DirectorySeparator " +
|
||||
"as the last character." +
|
||||
"\nSupplied Path: {0}, Filename: {1}",
|
||||
unityDirectoryPath);
|
||||
Debug.LogWarning(warningMessage);
|
||||
}
|
||||
|
||||
// Warn and strip filenames
|
||||
var filename = Path.GetFileName(unityDirectoryPath);
|
||||
if (!string.IsNullOrEmpty(filename))
|
||||
{
|
||||
var warningMessage = string.Format(
|
||||
"Path supplied to CreateDirectoriesInPath that appears to include a filename. It will be " +
|
||||
"stripped. A path that ends with a DirectorySeparate should be supplied. " +
|
||||
"\nSupplied Path: {0}, Filename: {1}",
|
||||
unityDirectoryPath,
|
||||
filename);
|
||||
Debug.LogWarning(warningMessage);
|
||||
|
||||
unityDirectoryPath = unityDirectoryPath.Replace(filename, string.Empty);
|
||||
}
|
||||
|
||||
var folders = unityDirectoryPath.Split(UnityDirectorySeparator);
|
||||
|
||||
// Error if path does NOT start from Assets
|
||||
if (folders.Length > 0 && folders[0] != "Assets")
|
||||
{
|
||||
var exceptionMessage = "AssetDatabaseUtility CreateDirectoriesInPath expects full Unity path, including 'Assets\\\". " +
|
||||
"Adding Assets to path.";
|
||||
throw new UnityException(exceptionMessage);
|
||||
}
|
||||
|
||||
string pathToFolder = string.Empty;
|
||||
foreach (var folder in folders)
|
||||
{
|
||||
// Don't check for or create empty folders
|
||||
if (string.IsNullOrEmpty(folder))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Create folders that don't exist
|
||||
pathToFolder = string.Concat(pathToFolder, folder);
|
||||
if (!UnityEditor.AssetDatabase.IsValidFolder(pathToFolder))
|
||||
{
|
||||
var pathToParent = System.IO.Directory.GetParent(pathToFolder).ToString();
|
||||
AssetDatabase.CreateFolder(pathToParent, folder);
|
||||
AssetDatabase.Refresh();
|
||||
}
|
||||
|
||||
pathToFolder = string.Concat(pathToFolder, UnityDirectorySeparator);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// get a valid path for the AssetDatabase from absolute path or subpath
|
||||
/// </summary>
|
||||
/// <param name="path">absolute path or subpath like "Resources"</param>
|
||||
/// <returns>path relative to the project directory</returns>
|
||||
public static string GetAssetPath(string path)
|
||||
{
|
||||
if (path == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
path = path.Remove(projectPath);
|
||||
|
||||
if (path.StartsWith("\\"))
|
||||
{
|
||||
path = path.Remove(0, 1);
|
||||
}
|
||||
|
||||
if (path.StartsWith("/"))
|
||||
{
|
||||
path = path.Remove(0, 1);
|
||||
}
|
||||
|
||||
if (!path.StartsWith("Assets") && !path.StartsWith("/Assets"))
|
||||
{
|
||||
path = Path.Combine("Assets", path);
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
// ================================================================================
|
||||
// string extensions
|
||||
// --------------------------------------------------------------------------------
|
||||
|
||||
private static string RemoveLastLetters(this string s, int letterCount)
|
||||
{
|
||||
if (string.IsNullOrEmpty(s))
|
||||
{
|
||||
return s;
|
||||
}
|
||||
|
||||
if (letterCount > s.Length)
|
||||
{
|
||||
letterCount = s.Length;
|
||||
}
|
||||
|
||||
return s.Remove(s.Length - letterCount);
|
||||
}
|
||||
|
||||
private static string Remove(this string s, string exactExpression)
|
||||
{
|
||||
return s.Replace(exactExpression, "");
|
||||
}
|
||||
}
|
||||
}
|
1014
Assets/AnimationImporter/Editor/Utilities/JSONObject.cs
Normal file
1014
Assets/AnimationImporter/Editor/Utilities/JSONObject.cs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,79 @@
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AnimationImporter
|
||||
{
|
||||
/// <summary>
|
||||
/// Utility functions for ScriptableObjects.
|
||||
/// </summary>
|
||||
public static class ScriptableObjectUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// Loads the save data from a Unity relative path. Returns null if the data doesn't exist.
|
||||
/// </summary>
|
||||
/// <returns>The saved data as a ScriptableObject, or null if not found.</returns>
|
||||
/// <param name="unityPathToFile">Unity path to file (e.g. "Assets/Resources/MyFile.asset")</param>
|
||||
/// <typeparam name="T">The ScriptableObject type</typeparam>
|
||||
public static T LoadSaveData<T> (string unityPathToFile) where T : ScriptableObject
|
||||
{
|
||||
// Path must contain Resources folder
|
||||
var resourcesFolder = string.Concat(
|
||||
AssetDatabaseUtility.UnityDirectorySeparator,
|
||||
AssetDatabaseUtility.ResourcesFolderName,
|
||||
AssetDatabaseUtility.UnityDirectorySeparator);
|
||||
if (!unityPathToFile.Contains(resourcesFolder))
|
||||
{
|
||||
var exceptionMessage = string.Format(
|
||||
"Failed to Load ScriptableObject of type, {0}, from path: {1}. " +
|
||||
"Path must begin with Assets and include a directory within the Resources folder.",
|
||||
typeof(T).ToString(),
|
||||
unityPathToFile);
|
||||
throw new UnityException(exceptionMessage);
|
||||
}
|
||||
|
||||
// Get Resource relative path - Resource path should only include folders underneath Resources and no file extension
|
||||
var resourceRelativePath = GetResourceRelativePath(unityPathToFile);
|
||||
|
||||
// Remove file extension
|
||||
var fileExtension = System.IO.Path.GetExtension(unityPathToFile);
|
||||
resourceRelativePath = resourceRelativePath.Replace(fileExtension, string.Empty);
|
||||
|
||||
return Resources.Load<T>(resourceRelativePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the saved data, stored as a ScriptableObject at the specified path. If the file or folders don't exist,
|
||||
/// it creates them.
|
||||
/// </summary>
|
||||
/// <returns>The saved data as a ScriptableObject.</returns>
|
||||
/// <param name="unityPathToFile">Unity path to file (e.g. "Assets/Resources/MyFile.asset")</param>
|
||||
/// <typeparam name="T">The ScriptableObject type</typeparam>
|
||||
public static T LoadOrCreateSaveData<T>(string unityPathToFile) where T : ScriptableObject
|
||||
{
|
||||
var loadedSettings = LoadSaveData<T>(unityPathToFile);
|
||||
if (loadedSettings == null)
|
||||
{
|
||||
loadedSettings = ScriptableObject.CreateInstance<T>();
|
||||
AssetDatabaseUtility.CreateAssetAndDirectories(loadedSettings, unityPathToFile);
|
||||
}
|
||||
|
||||
return loadedSettings;
|
||||
}
|
||||
|
||||
private static string GetResourceRelativePath(string unityPath)
|
||||
{
|
||||
var resourcesFolder = AssetDatabaseUtility.ResourcesFolderName + AssetDatabaseUtility.UnityDirectorySeparator;
|
||||
var pathToResources = unityPath.Substring(0, unityPath.IndexOf(resourcesFolder));
|
||||
|
||||
// Remove all folders leading up to the Resources folder
|
||||
pathToResources = unityPath.Replace(pathToResources, string.Empty);
|
||||
|
||||
// Remove the Resources folder
|
||||
pathToResources = pathToResources.Replace(resourcesFolder, string.Empty);
|
||||
|
||||
return pathToResources;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user