// Copyright (c) Pixel Crushers. All rights reserved. using UnityEngine; using System; using System.Collections.Generic; #if UNITY_WINRT using System.Linq.Expressions; using System.Reflection; #endif namespace PixelCrushers { /// /// Utility functions for creating ScriptableObjects. /// public static class ScriptableObjectUtility { /// /// Create a ScriptableObject of type T, calling the method Initialize() if present. /// /// The ScriptableObject type. /// The new ScriptableObject. public static T CreateScriptableObject() where T : ScriptableObject { var scriptableObject = ScriptableObject.CreateInstance() as T; InitializeScriptableObject(scriptableObject); return scriptableObject; } /// /// Create a ScriptableObject of a specified type, calling Initialize() if present. /// /// The ScriptableObject type. /// The new ScriptableObject. public static ScriptableObject CreateScriptableObject(Type type) { var scriptableObject = ScriptableObject.CreateInstance(type); InitializeScriptableObject(scriptableObject); return scriptableObject; } /// /// Calls Initialize() on a ScriptableObject if present. /// /// The ScriptableObject to initialize. public static void InitializeScriptableObject(ScriptableObject scriptableObject) { if (scriptableObject == null) return; var methodInfo = scriptableObject.GetType().GetMethod("Initialize"); if (methodInfo != null) methodInfo.Invoke(scriptableObject, null); } /// /// Makes a deep copy of a ScriptableObject list by instantiating copies of the /// list elements. /// /// List to clone. /// Optional reference to source object to report in log if operation fails. /// A second list containing new instances of the list elements. public static List CloneList(List original, UnityEngine.Object source = null) where T : ScriptableObject { var copy = new List(); if (original != null) { for (int i = 0; i < original.Count; i++) { if (original[i] != null && original[i] is T) { copy.Add(ScriptableObject.Instantiate(original[i]) as T); } else { if (Debug.isDebugBuild) { if (source != null) { Debug.LogWarning("CloneList<" + typeof(T).Name + ">: Element " + i + " is null in a list in " + source + ".", source); } else { Debug.LogWarning("CloneList<" + typeof(T).Name + ">: Element " + i + " is null."); } } copy.Add(null); } } } return copy; } #if UNITY_WINRT /// /// Given a lambda expression that calls a method, returns the method info. /// /// /// The expression. /// public static MethodInfo GetMethodInfo(Expression expression) { return GetMethodInfo((LambdaExpression)expression); } /// /// Given a lambda expression that calls a method, returns the method info. /// /// /// The expression. /// public static MethodInfo GetMethodInfo(Expression> expression) { return GetMethodInfo((LambdaExpression)expression); } /// /// Given a lambda expression that calls a method, returns the method info. /// /// /// The expression. /// public static MethodInfo GetMethodInfo(Expression> expression) { return GetMethodInfo((LambdaExpression)expression); } /// /// Given a lambda expression that calls a method, returns the method info. /// /// The expression. /// public static MethodInfo GetMethodInfo(LambdaExpression expression) { MethodCallExpression outermostExpression = expression.Body as MethodCallExpression; if (outermostExpression == null) { throw new ArgumentException("Invalid Expression. Expression should consist of a Method call only."); } return outermostExpression.Method; } #endif } }