├── .gitignore ├── LICENSE ├── README.md ├── Source ├── Utils.meta ├── Utils │ ├── Editor.meta │ ├── Editor │ │ ├── CreateVariableWizard.cs │ │ ├── CreateVariableWizard.cs.meta │ │ ├── EnableRxEditor.cs │ │ ├── EnableRxEditor.cs.meta │ │ ├── ExposableMonobehaviourEditor.cs │ │ ├── ExposableMonobehaviourEditor.cs.meta │ │ ├── ExposableScriptableObjectEditor.cs │ │ ├── ExposableScriptableObjectEditor.cs.meta │ │ ├── ExposeProperties.cs │ │ └── ExposeProperties.cs.meta │ ├── ExposableMonoBehaviour.cs │ ├── ExposableMonoBehaviour.cs.meta │ ├── ExposableScriptableObject.cs │ ├── ExposableScriptableObject.cs.meta │ ├── ExposePropertyAttribute.cs │ └── ExposePropertyAttribute.cs.meta ├── Variables.meta ├── Variables │ ├── BoolReference.cs │ ├── BoolReference.cs.meta │ ├── BoolVariable.cs │ ├── BoolVariable.cs.meta │ ├── Editor.meta │ ├── Editor │ │ ├── BoolReferenceDrawer.cs │ │ ├── BoolReferenceDrawer.cs.meta │ │ ├── BoolVariableDrawer.cs │ │ ├── BoolVariableDrawer.cs.meta │ │ ├── FloatReferenceDrawer.cs │ │ ├── FloatReferenceDrawer.cs.meta │ │ ├── FloatVariableDrawer.cs │ │ ├── FloatVariableDrawer.cs.meta │ │ ├── IntReferenceDrawer.cs │ │ ├── IntReferenceDrawer.cs.meta │ │ ├── IntVariableDrawer.cs │ │ ├── IntVariableDrawer.cs.meta │ │ ├── ReferenceDrawer.cs │ │ ├── ReferenceDrawer.cs.meta │ │ ├── StringReferenceDrawer.cs │ │ ├── StringReferenceDrawer.cs.meta │ │ ├── StringVariableDrawer.cs │ │ ├── StringVariableDrawer.cs.meta │ │ ├── VariableDrawer.cs │ │ ├── VariableDrawer.cs.meta │ │ ├── Vector2ReferenceDrawer.cs │ │ ├── Vector2ReferenceDrawer.cs.meta │ │ ├── Vector2VariableDrawer.cs │ │ ├── Vector2VariableDrawer.cs.meta │ │ ├── Vector3ReferenceDrawer.cs │ │ ├── Vector3ReferenceDrawer.cs.meta │ │ ├── Vector3VariableDrawer.cs │ │ └── Vector3VariableDrawer.cs.meta │ ├── FloatReference.cs │ ├── FloatReference.cs.meta │ ├── FloatVariable.cs │ ├── FloatVariable.cs.meta │ ├── IntReference.cs │ ├── IntReference.cs.meta │ ├── IntVariable.cs │ ├── IntVariable.cs.meta │ ├── Reference.cs │ ├── Reference.cs.meta │ ├── StringReference.cs │ ├── StringReference.cs.meta │ ├── StringVariable.cs │ ├── StringVariable.cs.meta │ ├── Variable.cs │ ├── Variable.cs.meta │ ├── Vector2Reference.cs │ ├── Vector2Reference.cs.meta │ ├── Vector2Variable.cs │ ├── Vector2Variable.cs.meta │ ├── Vector3Reference.cs │ ├── Vector3Reference.cs.meta │ ├── Vector3Variable.cs │ └── Vector3Variable.cs.meta ├── Views.meta └── Views │ ├── GameObjectToggle.cs │ ├── GameObjectToggle.cs.meta │ ├── VariableToggle.cs │ └── VariableToggle.cs.meta ├── Templates.meta └── Templates ├── template_Editor_ReferenceDrawer.txt ├── template_Editor_ReferenceDrawer.txt.meta ├── template_Editor_VariableDrawer.txt ├── template_Editor_VariableDrawer.txt.meta ├── template_Reference.txt ├── template_Reference.txt.meta ├── template_Variable.txt └── template_Variable.txt.meta /.gitignore: -------------------------------------------------------------------------------- 1 | [Ll]ibrary/ 2 | [Tt]emp/ 3 | [Oo]bj/ 4 | [Bb]uild/ 5 | [Bb]uilds/ 6 | Assets/AssetStoreTools* 7 | 8 | # Visual Studio cache directory 9 | .vs/ 10 | 11 | # Autogenerated VS/MD/Consulo solution and project files 12 | ExportedObj/ 13 | .consulo/ 14 | *.csproj 15 | *.unityproj 16 | *.sln 17 | *.suo 18 | *.tmp 19 | *.user 20 | *.userprefs 21 | *.pidb 22 | *.booproj 23 | *.svd 24 | *.pdb 25 | *.opendb 26 | 27 | # Unity3D generated meta files 28 | *.pidb.meta 29 | *.pdb.meta 30 | 31 | # Unity3D Generated File On Crash Reports 32 | sysinfo.txt 33 | 34 | # Builds 35 | *.apk 36 | *.unitypackage 37 | LICENSE.meta 38 | README.md.meta 39 | Source.meta 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Wolar-Games 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Scriptable Object Variables with UniRx 2 | This is my implementation of variables in Unity using scriptable objects as explained in [this](https://www.youtube.com/watch?v=raQ3iHhE_Kk&t=3289s) talk. I'm using it with [UniRx](https://github.com/neuecc/UniRx) but I have also implemented version that is without UniRx and is using C# Actions instead. By default you will get version without UniRx integration. See Integrations section for a guide how to enable UniRx integration (it's really easy). 3 | 4 | # Usage 5 | ## Creating Variable 6 | Simply right click into one of you folders in your project, select: Create -> Variable -> Select Type (there are some primitive types already contained in this library) 7 | ## Using Variable 8 | Anywhere in you script simply define reference to a variable like: 9 | ```C# 10 | public BoolReference myVariableName; 11 | ``` 12 | or the variable itself 13 | ```C# 14 | public BoolVariable myVariableName; 15 | ``` 16 | And use it anyway you wish. I prefer to use references for reading the values and variables for writing into them. 17 | 18 | 19 | # Creating new custom variables 20 | To simplify the process of creation of new variables and editor scripts for them I have implemented tool that help you with that 21 | - Use Tools -> Reactive Variables -> Create New (it uses current assembly, if you want to create variable for another assembly like Unitys Game Object, or you are using new Assembly Definition Files, use All Assembly option) 22 | - Pick folders where to store generated files 23 | - Please note that the editor scripts needs to be stored in Editor folder or assembly 24 | - Start typing the class name 25 | - Select class you want to create variables for and click confirm 26 | 27 | # Integrations 28 | - This project is integrated with UniRx - https://github.com/neuecc/UniRx 29 | - To enable this integration, use Tools -> Reactive Variables -> Enable UniRx Integration, this option needs to be enabled for each platform you want to use this integration with. 30 | -------------------------------------------------------------------------------- /Source/Utils.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 63818bc5db3384eec915832612f381e9 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Source/Utils/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e7247a65ae9f747ca920277eea5b76f9 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Source/Utils/Editor/CreateVariableWizard.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Reflection; 6 | using UnityEditor; 7 | using UnityEngine; 8 | using UnityEngine.Assertions; 9 | 10 | namespace WolarGames.Variables.Utils 11 | { 12 | public class CreateVariableWizard : ScriptableWizard 13 | { 14 | public string VariableType; 15 | 16 | public string StorePath; 17 | public string StoreEditorPath; 18 | 19 | public IEnumerable AllTypes; 20 | 21 | private string _newString; 22 | private string _filterString; 23 | private IEnumerable _filteredTypes; 24 | private string[] _filteredTypeNames; 25 | 26 | private int _selectedIndex = 0; 27 | private float _timeSinceLastChange = 0; 28 | public bool ImportAfterCreating = true; 29 | 30 | private const float TimeSinceLastChangeTreshold = 0.5f; 31 | 32 | private const string StorePathKey = "Wolargames.Variables.StorePath"; 33 | private const string EditorStorePathKey = "Wolargames.Variables.EditorStorePath"; 34 | private const string ImportingKey = "Wolargames.Variables.Importing"; 35 | 36 | [MenuItem("Tools/Reactive Variables/Create New", false, 1)] 37 | static void CreateWizardLocal() 38 | { 39 | CreateNewWizard(new [] 40 | { 41 | typeof(IntVariable).Assembly 42 | }); 43 | } 44 | 45 | [MenuItem("Tools/Reactive Variables/Create New (All assemblies)", false, 2)] 46 | static void CreateWizardAllAssemblies() 47 | { 48 | CreateNewWizard(AppDomain.CurrentDomain.GetAssemblies()); 49 | } 50 | 51 | private static void CreateNewWizard(Assembly[] assemblies) 52 | { 53 | var wizard = DisplayWizard("Create Variable", "Create"); 54 | wizard.StorePath = EditorPrefs.GetString(StorePathKey, null); 55 | wizard.StoreEditorPath = EditorPrefs.GetString(EditorStorePathKey, null); 56 | wizard.ImportAfterCreating = EditorPrefs.GetBool(ImportingKey, true); 57 | 58 | var allTypes = new List(); 59 | foreach (var type in assemblies) 60 | { 61 | var types = type.GetTypes(); 62 | allTypes.AddRange(types); 63 | } 64 | 65 | wizard.AllTypes = allTypes.OrderBy(type => type.Name); 66 | } 67 | 68 | void OnWizardCreate() 69 | { 70 | if (string.IsNullOrEmpty(StorePath)) 71 | { 72 | Debug.LogError("Please select store where to store created class"); 73 | return; 74 | } 75 | 76 | if (string.IsNullOrEmpty(StoreEditorPath)) 77 | { 78 | Debug.LogError("Please select store where to store created editor class"); 79 | return; 80 | } 81 | 82 | CreateFiles(); 83 | } 84 | 85 | protected override bool DrawWizardGUI() 86 | { 87 | var somethingChanged = false; 88 | 89 | // Path to folder 90 | EditorGUILayout.BeginHorizontal(); 91 | EditorGUILayout.BeginVertical(); 92 | GUILayout.Label("Where to store class:", EditorStyles.boldLabel); 93 | GUILayout.Label(string.IsNullOrEmpty(StorePath) ? "Please pick" : StorePath ); 94 | EditorGUILayout.EndVertical(); 95 | if (GUILayout.Button("Select")) 96 | { 97 | string path = EditorUtility.OpenFolderPanel("Select folder", "", ""); 98 | if (path != null) 99 | { 100 | EditorPrefs.SetString(StorePathKey, path); 101 | StorePath = path; 102 | somethingChanged = true; 103 | } 104 | } 105 | EditorGUILayout.EndHorizontal(); 106 | 107 | EditorGUILayout.Space(); 108 | 109 | EditorGUILayout.BeginHorizontal(); 110 | EditorGUILayout.BeginVertical(); 111 | GUILayout.Label("Where to store editor class:", EditorStyles.boldLabel); 112 | GUILayout.Label(string.IsNullOrEmpty(StoreEditorPath) ? "Please pick" : StoreEditorPath); 113 | EditorGUILayout.EndVertical(); 114 | if (GUILayout.Button("Select")) 115 | { 116 | string path = EditorUtility.OpenFolderPanel("Select folder", "", ""); 117 | if (path != null) 118 | { 119 | EditorPrefs.SetString(EditorStorePathKey, path); 120 | StoreEditorPath = path; 121 | somethingChanged = true; 122 | } 123 | } 124 | EditorGUILayout.EndHorizontal(); 125 | 126 | GUILayout.Label("Filter:", EditorStyles.boldLabel); 127 | var newString = GUILayout.TextField(_newString); 128 | if (newString != _newString) 129 | { 130 | _timeSinceLastChange = 0; 131 | _newString = newString; 132 | } 133 | 134 | if ((_timeSinceLastChange >= TimeSinceLastChangeTreshold && _newString != _filterString)) 135 | { 136 | _filterString = _newString; 137 | if (_filterString.Length >= 3) 138 | { 139 | _filteredTypes = AllTypes.Where(type => type.Name.Contains(_filterString)); 140 | _filteredTypeNames = _filteredTypes.Select(type => type.Name).ToArray(); 141 | } 142 | } 143 | 144 | _timeSinceLastChange += Time.deltaTime; 145 | 146 | if (_filterString != null && _filterString.Length >= 3 && _filteredTypeNames != null) 147 | { 148 | _selectedIndex = EditorGUILayout.Popup("Class", _selectedIndex, _filteredTypeNames); 149 | isValid = _filteredTypeNames.Length > 0; 150 | } 151 | else 152 | { 153 | GUILayout.Label("Please enter at leasr 3 characters into filter"); 154 | isValid = false; 155 | } 156 | 157 | var newValue = GUILayout.Toggle(ImportAfterCreating, "Import after created"); 158 | if (newValue != ImportAfterCreating) 159 | { 160 | ImportAfterCreating = newValue; 161 | EditorPrefs.SetBool(ImportingKey, ImportAfterCreating); 162 | } 163 | 164 | return somethingChanged; 165 | } 166 | 167 | private void CreateFiles() 168 | { 169 | CreateScript("template_Variable", StorePath, "Variable"); 170 | CreateScript("template_Reference", StorePath, "Reference"); 171 | CreateScript("template_Editor_VariableDrawer", StoreEditorPath, "VariableDrawer"); 172 | CreateScript("template_Editor_ReferenceDrawer", StoreEditorPath, "ReferenceDrawer"); 173 | } 174 | 175 | private void CreateScript(string templaName, string targetFolder, string filenameSuffix) 176 | { 177 | Assert.IsTrue(Directory.Exists(targetFolder), targetFolder + " does not exist"); 178 | 179 | var guids = AssetDatabase.FindAssets(templaName); 180 | Assert.IsTrue(guids.Length > 0, string.Format("Missing template: \"{0}\"", templaName)); 181 | Assert.IsTrue(guids.Length < 2, string.Format("To many templates for name: \"{0}\"", templaName)); 182 | 183 | var firstGuid = guids.First(); 184 | var templatePath = AssetDatabase.GUIDToAssetPath(firstGuid); 185 | try 186 | { 187 | var type = _filteredTypes.ElementAt(_selectedIndex); 188 | var template = File.ReadAllText(templatePath); 189 | template = template.Replace("#VARIABLE#", type.Name); 190 | 191 | if (!string.IsNullOrEmpty(type.Namespace)) 192 | { 193 | template = template.Replace("#NAMESPACE#", type.Namespace); 194 | template = template.Replace("#NAMESPACE_KEYWORD#", "namespace"); 195 | template = template.Replace("#NAMESPACE_OPEN#", "{"); 196 | template = template.Replace("#NAMESPACE_CLOSE#", "}"); 197 | } 198 | else 199 | { 200 | template = template.Replace("#NAMESPACE#", string.Empty); 201 | template = template.Replace("#NAMESPACE_KEYWORD#", string.Empty); 202 | template = template.Replace("#NAMESPACE_OPEN#", string.Empty); 203 | template = template.Replace("#NAMESPACE_CLOSE#", string.Empty); 204 | } 205 | 206 | var filename = string.Format("{0}{1}.cs", type.Name, filenameSuffix); 207 | var path = Path.Combine(targetFolder, filename); 208 | File.WriteAllText(path, template); 209 | 210 | if (path.StartsWith(Application.dataPath)) { 211 | var relativePath = "Assets" + path.Substring(Application.dataPath.Length); 212 | Debug.LogFormat("Created file at path: {0}", relativePath); 213 | if (ImportAfterCreating) { 214 | AssetDatabase.ImportAsset(relativePath, ImportAssetOptions.Default); 215 | } 216 | } 217 | } 218 | catch (Exception e) 219 | { 220 | Debug.LogError(e); 221 | } 222 | } 223 | } 224 | } -------------------------------------------------------------------------------- /Source/Utils/Editor/CreateVariableWizard.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8fa49872bde64961af8c6031b54c40b9 3 | timeCreated: 1530650557 -------------------------------------------------------------------------------- /Source/Utils/Editor/EnableRxEditor.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using System.Linq; 3 | using UnityEditor; 4 | using UnityEngine; 5 | using UnityEngine.Assertions; 6 | 7 | namespace WolarGames.Variables.Utils 8 | { 9 | public class EnableRxEditor: MonoBehaviour 10 | { 11 | [MenuItem("Tools/Reactive Variables/Enable UniRx Integration", false, 15)] 12 | static void Enable() 13 | { 14 | var symbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(GetCurrentBuildGroup()); 15 | symbols += ";REACTIVE_VARIABLE_RX_ENABLED"; 16 | PlayerSettings.SetScriptingDefineSymbolsForGroup(GetCurrentBuildGroup(), symbols); 17 | } 18 | 19 | [MenuItem("Tools/Reactive Variables/Disable UniRx Integration", false, 16)] 20 | static void Disable() 21 | { 22 | var symbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(GetCurrentBuildGroup()); 23 | symbols = symbols.Replace("REACTIVE_VARIABLE_RX_ENABLED", string.Empty); 24 | PlayerSettings.SetScriptingDefineSymbolsForGroup(GetCurrentBuildGroup(), symbols); 25 | } 26 | 27 | [MenuItem("Tools/Reactive Variables/Enable UniRx Integration", true, 15)] 28 | static bool CanEnable() 29 | { 30 | return !ContainsPreprocessor(); 31 | } 32 | 33 | [MenuItem("Tools/Reactive Variables/Disable UniRx Integration", true, 16)] 34 | static bool CanDisable() 35 | { 36 | return ContainsPreprocessor(); 37 | } 38 | 39 | private static bool ContainsPreprocessor() 40 | { 41 | // TODO: Improve this 42 | var symbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(GetCurrentBuildGroup()); 43 | return symbols.Contains("REACTIVE_VARIABLE_RX_ENABLED"); 44 | } 45 | 46 | private static BuildTargetGroup GetCurrentBuildGroup() 47 | { 48 | var target = EditorUserBuildSettings.activeBuildTarget; 49 | return BuildPipeline.GetBuildTargetGroup(target); 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /Source/Utils/Editor/EnableRxEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 12d453dd571549d1a90a269f245f8d4e 3 | timeCreated: 1530736584 -------------------------------------------------------------------------------- /Source/Utils/Editor/ExposableMonobehaviourEditor.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | 3 | // Idea taken from: http://wiki.unity3d.com/index.php/ExposePropertiesInInspector_Generic 4 | // Original author: Venryx (venryx) (variant from Mift (mift) ) 5 | // Variant author: Tiago Roldão(Tiago Roldão) 6 | 7 | namespace WolarGames.Variables.Utils 8 | { 9 | [CustomEditor(typeof(ExposableMonoBehaviour), true)] 10 | public class ExposableMonobehaviourEditor : Editor 11 | { 12 | ExposableMonoBehaviour m_Instance; 13 | PropertyField[] m_fields; 14 | 15 | public virtual void OnEnable() { 16 | m_Instance = target as ExposableMonoBehaviour; 17 | m_fields = ExposeProperties.GetProperties(m_Instance); 18 | } 19 | 20 | public override void OnInspectorGUI() { 21 | if (m_Instance == null) 22 | return; 23 | DrawDefaultInspector(); 24 | ExposeProperties.Expose(m_fields); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Source/Utils/Editor/ExposableMonobehaviourEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 18b68c1910a6a437696dc09657a6a6e3 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Source/Utils/Editor/ExposableScriptableObjectEditor.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | 3 | // Idea taken from: http://wiki.unity3d.com/index.php/ExposePropertiesInInspector_Generic 4 | // Original author: Venryx (venryx) (variant from Mift (mift) ) 5 | // Variant author: Tiago Roldão(Tiago Roldão) 6 | 7 | namespace WolarGames.Variables.Utils 8 | { 9 | [CustomEditor(typeof(ExposableScriptableObject), true)] 10 | public class ExposableScriptableObjectEditor : Editor 11 | { 12 | ExposableScriptableObject m_Instance; 13 | PropertyField[] m_fields; 14 | 15 | public virtual void OnEnable() { 16 | m_Instance = target as ExposableScriptableObject; 17 | m_fields = ExposeProperties.GetProperties(m_Instance); 18 | } 19 | 20 | public override void OnInspectorGUI() { 21 | if (m_Instance == null) 22 | return; 23 | this.DrawDefaultInspector(); 24 | ExposeProperties.Expose(m_fields); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Source/Utils/Editor/ExposableScriptableObjectEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 945a35ec4b96d4dd3b3197678f91d65c 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Source/Utils/Editor/ExposeProperties.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | using UnityEngine; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Reflection; 6 | 7 | // Taken from http://wiki.unity3d.com/index.php/ExposePropertiesInInspector_SetOnlyWhenChanged 8 | // Original author: Mift (mift) 9 | // Variant author: Venryx(venryx) 10 | 11 | namespace WolarGames.Variables.Utils 12 | { 13 | public static class ExposeProperties 14 | { 15 | public static void Expose(PropertyField[] properties) { 16 | var emptyOptions = new GUILayoutOption[0]; 17 | EditorGUILayout.BeginVertical(emptyOptions); 18 | foreach (PropertyField field in properties) { 19 | EditorGUILayout.BeginHorizontal(emptyOptions); 20 | if (field.Type == SerializedPropertyType.Integer) { 21 | var oldValue = (int)field.GetValue(); 22 | var newValue = EditorGUILayout.IntField(field.Name, oldValue, emptyOptions); 23 | if (oldValue != newValue) 24 | field.SetValue(newValue); 25 | } else if (field.Type == SerializedPropertyType.Float) { 26 | var oldValue = (float)field.GetValue(); 27 | var newValue = EditorGUILayout.FloatField(field.Name, oldValue, emptyOptions); 28 | if (oldValue != newValue) 29 | field.SetValue(newValue); 30 | } else if (field.Type == SerializedPropertyType.Boolean) { 31 | var oldValue = (bool)field.GetValue(); 32 | var newValue = EditorGUILayout.Toggle(field.Name, oldValue, emptyOptions); 33 | if (oldValue != newValue) 34 | field.SetValue(newValue); 35 | } else if (field.Type == SerializedPropertyType.String) { 36 | var oldValue = (string)field.GetValue(); 37 | var newValue = EditorGUILayout.TextField(field.Name, oldValue, emptyOptions); 38 | if (oldValue != newValue) 39 | field.SetValue(newValue); 40 | } else if (field.Type == SerializedPropertyType.Vector2) { 41 | var oldValue = (Vector2)field.GetValue(); 42 | var newValue = EditorGUILayout.Vector2Field(field.Name, oldValue, emptyOptions); 43 | if (oldValue != newValue) 44 | field.SetValue(newValue); 45 | } else if (field.Type == SerializedPropertyType.Vector3) { 46 | var oldValue = (Vector3)field.GetValue(); 47 | var newValue = EditorGUILayout.Vector3Field(field.Name, oldValue, emptyOptions); 48 | if (oldValue != newValue) 49 | field.SetValue(newValue); 50 | } else if (field.Type == SerializedPropertyType.Enum) { 51 | var oldValue = (Enum)field.GetValue(); 52 | var newValue = EditorGUILayout.EnumPopup(field.Name, oldValue, emptyOptions); 53 | if (oldValue != newValue) 54 | field.SetValue(newValue); 55 | } else if (field.Type == SerializedPropertyType.ObjectReference) { 56 | UnityEngine.Object oldValue = (UnityEngine.Object)field.GetValue(); 57 | UnityEngine.Object newValue = EditorGUILayout.ObjectField(field.Name, oldValue, field.Info.PropertyType, true); 58 | if (oldValue != newValue) 59 | field.SetValue(newValue); 60 | } 61 | EditorGUILayout.EndHorizontal(); 62 | } 63 | EditorGUILayout.EndVertical(); 64 | } 65 | 66 | public static PropertyField[] GetProperties(object obj) { 67 | var fields = new List(); 68 | 69 | PropertyInfo[] infos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); 70 | 71 | foreach (PropertyInfo info in infos) { 72 | if (!(info.CanRead && info.CanWrite)) 73 | continue; 74 | 75 | object[] attributes = info.GetCustomAttributes(true); 76 | 77 | bool isExposed = false; 78 | foreach (object o in attributes) 79 | if (o.GetType() == typeof(ExposePropertyAttribute)) { 80 | isExposed = true; 81 | break; 82 | } 83 | if (!isExposed) 84 | continue; 85 | 86 | var type = SerializedPropertyType.Integer; 87 | if (PropertyField.GetPropertyType(info, out type)) { 88 | var field = new PropertyField(obj, info, type); 89 | fields.Add(field); 90 | } 91 | } 92 | 93 | return fields.ToArray(); 94 | } 95 | } 96 | 97 | public class PropertyField 98 | { 99 | object obj; 100 | PropertyInfo info; 101 | SerializedPropertyType type; 102 | 103 | MethodInfo getter; 104 | MethodInfo setter; 105 | 106 | public PropertyInfo Info 107 | { 108 | get { return info; } 109 | } 110 | 111 | public SerializedPropertyType Type 112 | { 113 | get { return type; } 114 | } 115 | 116 | public String Name 117 | { 118 | get { return ObjectNames.NicifyVariableName(info.Name); } 119 | } 120 | 121 | public PropertyField(object obj, PropertyInfo info, SerializedPropertyType type) { 122 | this.obj = obj; 123 | this.info = info; 124 | this.type = type; 125 | 126 | getter = this.info.GetGetMethod(); 127 | setter = this.info.GetSetMethod(); 128 | } 129 | 130 | public object GetValue() { return getter.Invoke(obj, null); } 131 | public void SetValue(object value) { 132 | Undo.RecordObject(obj as UnityEngine.Object, "Exposed property changed"); 133 | setter.Invoke(obj, new[] { value }); 134 | } 135 | 136 | public static bool GetPropertyType(PropertyInfo info, out SerializedPropertyType propertyType) { 137 | Type type = info.PropertyType; 138 | propertyType = SerializedPropertyType.Generic; 139 | if (type == typeof(int)) 140 | propertyType = SerializedPropertyType.Integer; 141 | else if (type == typeof(float)) 142 | propertyType = SerializedPropertyType.Float; 143 | else if (type == typeof(bool)) 144 | propertyType = SerializedPropertyType.Boolean; 145 | else if (type == typeof(string)) 146 | propertyType = SerializedPropertyType.String; 147 | else if (type == typeof(Vector2)) 148 | propertyType = SerializedPropertyType.Vector2; 149 | else if (type == typeof(Vector3)) 150 | propertyType = SerializedPropertyType.Vector3; 151 | else if (type.IsEnum) 152 | propertyType = SerializedPropertyType.Enum; 153 | else if (typeof(MonoBehaviour).IsAssignableFrom(type)) 154 | propertyType = SerializedPropertyType.ObjectReference; 155 | else if (typeof(ScriptableObject).IsAssignableFrom(type)) 156 | propertyType = SerializedPropertyType.ObjectReference; 157 | return propertyType != SerializedPropertyType.Generic; 158 | } 159 | } 160 | } -------------------------------------------------------------------------------- /Source/Utils/Editor/ExposeProperties.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e3bf0c94c5a974bb69b376dbca5f1695 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Source/Utils/ExposableMonoBehaviour.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | // Idea taken from: http://wiki.unity3d.com/index.php/ExposePropertiesInInspector_Generic 4 | // Original author: Venryx (venryx) (variant from Mift (mift) ) 5 | // Variant author: Tiago Roldão(Tiago Roldão) 6 | 7 | namespace WolarGames.Variables.Utils 8 | { 9 | public class ExposableMonoBehaviour : MonoBehaviour 10 | { } 11 | } -------------------------------------------------------------------------------- /Source/Utils/ExposableMonoBehaviour.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 89f91a94d565a49d3bc8282d870dd63a 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Source/Utils/ExposableScriptableObject.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | // Idea taken from: http://wiki.unity3d.com/index.php/ExposePropertiesInInspector_Generic 4 | // Original author: Venryx (venryx) (variant from Mift (mift) ) 5 | // Variant author: Tiago Roldão(Tiago Roldão) 6 | 7 | namespace WolarGames.Variables.Utils 8 | { 9 | /// Simplification that allows any scriptable object inheriting from this one to expose it's properties by using [ExposeProperty] tag 10 | public class ExposableScriptableObject : ScriptableObject 11 | { } 12 | } -------------------------------------------------------------------------------- /Source/Utils/ExposableScriptableObject.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4890b9c0af49842ecb0bebd1ab2e7ca2 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Source/Utils/ExposePropertyAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | // Taken from http://wiki.unity3d.com/index.php/ExposePropertiesInInspector_SetOnlyWhenChanged 4 | // Original author: Mift (mift) 5 | // Variant author: Venryx(venryx) 6 | 7 | namespace WolarGames.Variables.Utils 8 | { 9 | [AttributeUsage(AttributeTargets.Property)] public class ExposePropertyAttribute : Attribute { } 10 | } -------------------------------------------------------------------------------- /Source/Utils/ExposePropertyAttribute.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7f18381725d26414f964a6cd15e6befa 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Source/Variables.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 870df8b2798124458b18a93466480a29 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Source/Variables/BoolReference.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [Serializable] 6 | public class BoolReference : Reference 7 | { 8 | public BoolVariable VariableReference; 9 | 10 | public override Variable Variable 11 | { 12 | get { 13 | return VariableReference; 14 | } 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Source/Variables/BoolReference.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: bf27369d60e04e598eb3c441bca5a998 3 | timeCreated: 1530649811 -------------------------------------------------------------------------------- /Source/Variables/BoolVariable.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CreateAssetMenu(menuName = "Variables/Bool", order = 50)] 6 | public class BoolVariable : Variable 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/BoolVariable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 773c8810a15f4df4a8129199cce77f64 3 | timeCreated: 1530649905 -------------------------------------------------------------------------------- /Source/Variables/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: deb00d63600c04af7b94e4480f08fd0b 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Source/Variables/Editor/BoolReferenceDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CustomPropertyDrawer(typeof(BoolReference))] 6 | public class BoolReferenceDrawer : ReferenceDrawer 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/BoolReferenceDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9be4661aa2564c73a2601f5d16f2596f 3 | timeCreated: 1530649979 -------------------------------------------------------------------------------- /Source/Variables/Editor/BoolVariableDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CustomEditor(typeof(BoolVariable))] 6 | public class BoolVariableDrawer : VariableDrawer 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/BoolVariableDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 99d7d478d915407599db7b65c1225f4d 3 | timeCreated: 1530650041 -------------------------------------------------------------------------------- /Source/Variables/Editor/FloatReferenceDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CustomPropertyDrawer(typeof(FloatReference))] 6 | public class FloatReferenceDrawer : ReferenceDrawer 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/FloatReferenceDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8994614fc5eb4657af8fe96d133c5484 3 | timeCreated: 1530649987 -------------------------------------------------------------------------------- /Source/Variables/Editor/FloatVariableDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CustomEditor(typeof(FloatVariable))] 6 | public class FloatVariableDrawer : VariableDrawer 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/FloatVariableDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d4a32e034dc545aea80849ed0fbe11f8 3 | timeCreated: 1530650030 -------------------------------------------------------------------------------- /Source/Variables/Editor/IntReferenceDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CustomPropertyDrawer(typeof(IntReference))] 6 | public class IntReferenceDrawer : ReferenceDrawer 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/IntReferenceDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d998242f38e6445fa5a7532f36aeedea 3 | timeCreated: 1530649995 -------------------------------------------------------------------------------- /Source/Variables/Editor/IntVariableDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CustomEditor(typeof(IntVariable))] 6 | public class IntVariableDrawer : VariableDrawer 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/IntVariableDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4cd6dc46b0a04826a97249bb3d9a5cc6 3 | timeCreated: 1530650048 -------------------------------------------------------------------------------- /Source/Variables/Editor/ReferenceDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | using UnityEngine; 3 | 4 | // Credits: Taken from https://github.com/roboryantron/Unite2017 5 | 6 | namespace WolarGames.Variables 7 | { 8 | public abstract class ReferenceDrawer : PropertyDrawer 9 | { 10 | /// 11 | /// Options to display in the popup to select constant or variable. 12 | /// 13 | private readonly string[] popupOptions = 14 | { "Use Constant", "Use Variable" }; 15 | 16 | /// Cached style to use to draw the popup button. 17 | private GUIStyle popupStyle; 18 | 19 | public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { 20 | if (popupStyle == null) { 21 | popupStyle = new GUIStyle(GUI.skin.GetStyle("PaneOptions")); 22 | popupStyle.imagePosition = ImagePosition.ImageOnly; 23 | } 24 | 25 | label = EditorGUI.BeginProperty(position, label, property); 26 | position = EditorGUI.PrefixLabel(position, label); 27 | 28 | EditorGUI.BeginChangeCheck(); 29 | 30 | // Get properties 31 | SerializedProperty useConstant = property.FindPropertyRelative("UseConstant"); 32 | SerializedProperty constantValue = property.FindPropertyRelative("ConstantValue"); 33 | SerializedProperty variable = property.FindPropertyRelative("VariableReference"); 34 | 35 | // Calculate rect for configuration button 36 | Rect buttonRect = new Rect(position); 37 | buttonRect.yMin += popupStyle.margin.top; 38 | buttonRect.width = popupStyle.fixedWidth + popupStyle.margin.right; 39 | position.xMin = buttonRect.xMax; 40 | 41 | // Store old indent level and set it to 0, the PrefixLabel takes care of it 42 | int indent = EditorGUI.indentLevel; 43 | EditorGUI.indentLevel = 0; 44 | 45 | int result = EditorGUI.Popup(buttonRect, useConstant.boolValue ? 0 : 1, popupOptions, popupStyle); 46 | 47 | useConstant.boolValue = result == 0; 48 | 49 | EditorGUI.PropertyField(position, 50 | useConstant.boolValue ? constantValue : variable, 51 | GUIContent.none); 52 | 53 | if (EditorGUI.EndChangeCheck()) 54 | property.serializedObject.ApplyModifiedProperties(); 55 | 56 | EditorGUI.indentLevel = indent; 57 | EditorGUI.EndProperty(); 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/ReferenceDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 028d5eab3bd26465192b8036377f18f8 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Source/Variables/Editor/StringReferenceDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CustomPropertyDrawer(typeof(StringReference))] 6 | public class StringReferenceDrawer : ReferenceDrawer 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/StringReferenceDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 306eb9e8a88542f98adad560fbc7a5be 3 | timeCreated: 1530650002 -------------------------------------------------------------------------------- /Source/Variables/Editor/StringVariableDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CustomEditor(typeof(StringVariable))] 6 | public class StringVariableDrawer : VariableDrawer 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/StringVariableDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ab5b28e84cca4f13877f68b6ec6ad9b4 3 | timeCreated: 1530650058 -------------------------------------------------------------------------------- /Source/Variables/Editor/VariableDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEditor; 3 | using WolarGames.Variables.Utils; 4 | #if REACTIVE_VARIABLE_RX_ENABLED 5 | using UniRx; 6 | #endif 7 | using System; 8 | 9 | namespace WolarGames.Variables 10 | { 11 | [CustomEditor(typeof(Variable<>))] 12 | public class VariableDrawer : Editor 13 | { 14 | private Variable _target; 15 | private PropertyField[] _fields; 16 | 17 | private IDisposable _disposable; 18 | 19 | public void OnEnable() { 20 | _target = target as Variable; 21 | _fields = ExposeProperties.GetProperties(_target); 22 | 23 | #if REACTIVE_VARIABLE_RX_ENABLED 24 | _disposable = _target.AsObservable().Subscribe(value => { 25 | Repaint(); 26 | }); 27 | #else 28 | _target.OnValueChanged += HandleValueChanged; 29 | #endif 30 | } 31 | 32 | public void OnDisable() { 33 | #if REACTIVE_VARIABLE_RX_ENABLED 34 | if (_disposable != null) { 35 | _disposable.Dispose(); 36 | _disposable = null; 37 | } 38 | #else 39 | _target.OnValueChanged -= HandleValueChanged; 40 | #endif 41 | } 42 | 43 | private void HandleValueChanged(T value) 44 | { 45 | Repaint(); 46 | } 47 | 48 | public override void OnInspectorGUI() { 49 | 50 | if (_target == null) 51 | return; 52 | 53 | DrawDefaultInspector(); 54 | ExposeProperties.Expose(_fields); 55 | 56 | if (_target != null && _target.CurrentValue != null && _target.DefaultValue != null && !_target.CurrentValue.Equals(_target.DefaultValue)) { 57 | if (GUILayout.Button("Current -> Default")) { 58 | _target.DefaultValue = _target.CurrentValue; 59 | } 60 | } 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/VariableDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 09554440821ce4b7abedc37ebafd2340 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Source/Variables/Editor/Vector2ReferenceDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CustomPropertyDrawer(typeof(Vector2Reference))] 6 | public class Vector2ReferenceDrawer : ReferenceDrawer 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/Vector2ReferenceDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e5526ce0f5ca4dfb87fb4447ccce34de 3 | timeCreated: 1530650017 -------------------------------------------------------------------------------- /Source/Variables/Editor/Vector2VariableDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | using UnityEngine; 3 | 4 | namespace WolarGames.Variables 5 | { 6 | [CustomEditor(typeof(Vector2Variable))] 7 | public class Vector2VariableDrawer : VariableDrawer 8 | { } 9 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/Vector2VariableDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 914c1857288b4a1c948a0bacdc76a0e1 3 | timeCreated: 1530650072 -------------------------------------------------------------------------------- /Source/Variables/Editor/Vector3ReferenceDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CustomPropertyDrawer(typeof(Vector3Reference))] 6 | public class Vector3ReferenceDrawer : ReferenceDrawer 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/Vector3ReferenceDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0d756bf24a1b48e997a874da2c540449 3 | timeCreated: 1530650010 -------------------------------------------------------------------------------- /Source/Variables/Editor/Vector3VariableDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | using UnityEngine; 3 | 4 | namespace WolarGames.Variables 5 | { 6 | [CustomEditor(typeof(Vector3Variable))] 7 | public class Vector3VariableDrawer : VariableDrawer 8 | { } 9 | } -------------------------------------------------------------------------------- /Source/Variables/Editor/Vector3VariableDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2524d8af17974e1a9a6833e49ea5124b 3 | timeCreated: 1530650064 -------------------------------------------------------------------------------- /Source/Variables/FloatReference.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [Serializable] 6 | public class FloatReference : Reference 7 | { 8 | public FloatVariable VariableReference; 9 | 10 | public override Variable Variable 11 | { 12 | get { 13 | return VariableReference; 14 | } 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Source/Variables/FloatReference.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2d1587d3197a4cfba1ddfa7bce359806 3 | timeCreated: 1530649829 -------------------------------------------------------------------------------- /Source/Variables/FloatVariable.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CreateAssetMenu(menuName = "Variables/Float", order = 52)] 6 | public class FloatVariable : Variable 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/FloatVariable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b012c1d050614689a4cc579f0608781a 3 | timeCreated: 1530649914 -------------------------------------------------------------------------------- /Source/Variables/IntReference.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [Serializable] 6 | public class IntReference : Reference 7 | { 8 | public IntVariable VariableReference; 9 | 10 | public override Variable Variable 11 | { 12 | get { 13 | return VariableReference; 14 | } 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Source/Variables/IntReference.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8e7f2f71334d4bfd99845017b09cddf8 3 | timeCreated: 1530649845 -------------------------------------------------------------------------------- /Source/Variables/IntVariable.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CreateAssetMenu(menuName = "Variables/Int", order = 51)] 6 | public class IntVariable : Variable 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/IntVariable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4756954df42c485b8b642c25a6278bc4 3 | timeCreated: 1530649922 -------------------------------------------------------------------------------- /Source/Variables/Reference.cs: -------------------------------------------------------------------------------- 1 | #if REACTIVE_VARIABLE_RX_ENABLED 2 | using UniRx; 3 | #endif 4 | using System; 5 | using UnityEngine.Assertions; 6 | 7 | namespace WolarGames.Variables 8 | { 9 | /// Generic base variable reference that all other variable references inherit 10 | /// Can (should) only be used for getting values, not setting them 11 | public abstract class Reference 12 | { 13 | public bool UseConstant = true; 14 | public T ConstantValue; 15 | public virtual Variable Variable { 16 | get { 17 | return null; 18 | } 19 | } 20 | 21 | public Reference() { } 22 | 23 | public Reference(T value): this() { 24 | UseConstant = true; 25 | ConstantValue = value; 26 | } 27 | 28 | #if REACTIVE_VARIABLE_RX_ENABLED 29 | public IObservable AsObservable() 30 | { 31 | Assert.IsTrue(UseConstant || Variable != null, "Using variable value with no variable assigned"); 32 | return UseConstant ? Observable.Return(ConstantValue) : Variable.AsObservable(); 33 | } 34 | #else 35 | 36 | public Action OnValueChanged 37 | { 38 | get 39 | { 40 | if (UseConstant) 41 | { 42 | return new Action(_ => 43 | { 44 | 45 | }); 46 | } 47 | else 48 | { 49 | return Variable.OnValueChanged; 50 | } 51 | } 52 | set 53 | { 54 | if (!UseConstant) 55 | { 56 | Variable.OnValueChanged = value; 57 | } 58 | } 59 | } 60 | #endif 61 | 62 | public T Value 63 | { 64 | get 65 | { 66 | return UseConstant ? ConstantValue : Variable.CurrentValue;; 67 | } 68 | } 69 | 70 | public static implicit operator T(Reference reference) 71 | { 72 | return reference.Value; 73 | } 74 | } 75 | } -------------------------------------------------------------------------------- /Source/Variables/Reference.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 69787a066ee9e4e65a2dfc21ad40547e 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Source/Variables/StringReference.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [Serializable] 6 | public class StringReference : Reference 7 | { 8 | public StringVariable VariableReference; 9 | 10 | public override Variable Variable 11 | { 12 | get { 13 | return VariableReference; 14 | } 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Source/Variables/StringReference.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5b35d681bb9d4288b3b99fab0a9ba4cc 3 | timeCreated: 1530649865 -------------------------------------------------------------------------------- /Source/Variables/StringVariable.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CreateAssetMenu(menuName = "Variables/String", order = 53)] 6 | public class StringVariable : Variable 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/StringVariable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4082dcef11d04494a5c919392763b325 3 | timeCreated: 1530649930 -------------------------------------------------------------------------------- /Source/Variables/Variable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | using WolarGames.Variables.Utils; 4 | #if REACTIVE_VARIABLE_RX_ENABLED 5 | using UniRx; 6 | #endif 7 | using System.Collections.Generic; 8 | 9 | namespace WolarGames.Variables 10 | { 11 | /// Generic base variable that all other variables inherit 12 | public abstract class Variable : ScriptableObject 13 | { 14 | // This used to be #if UNITY_EDITOR but there was a problem with memory layout on desktop platforms 15 | [Multiline] 16 | public string DeveloperDescription = ""; 17 | 18 | /// Default value of the variable, exposed in editor if T is serializable, should not be changed from game code without a good reason 19 | public T DefaultValue; 20 | 21 | // If this is set to true, setting the same value will notify the observers 22 | [HideInInspector] 23 | public bool AllowValueRepeating; 24 | 25 | private T _currentValue; 26 | /// Current value of the variable 27 | [ExposeProperty] 28 | public T CurrentValue { 29 | get { 30 | return _currentValue; 31 | } 32 | set { 33 | // TODO: Make the comparer setable 34 | if (AllowValueRepeating || !EqualityComparer.Default.Equals(_currentValue, value)) { 35 | _currentValue = value; 36 | #if REACTIVE_VARIABLE_RX_ENABLED 37 | if (_publisher != null) { 38 | _publisher.OnNext(value); 39 | } 40 | #else 41 | if (OnValueChanged != null) { 42 | OnValueChanged(value); 43 | } 44 | #endif 45 | } 46 | } 47 | } 48 | 49 | #if REACTIVE_VARIABLE_RX_ENABLED 50 | [NonSerialized] 51 | private BehaviorSubject _publisher; 52 | 53 | public IObservable AsObservable() { 54 | if (_publisher == null) { 55 | _publisher = new BehaviorSubject(_currentValue); 56 | } 57 | return _publisher; 58 | } 59 | #else 60 | public Action OnValueChanged; 61 | #endif 62 | 63 | public void SetValue(Variable value) { 64 | CurrentValue = value.CurrentValue; 65 | } 66 | 67 | private void OnEnable() { 68 | CurrentValue = DefaultValue; 69 | } 70 | 71 | public static implicit operator T(Variable variable) { 72 | if (variable == null) { 73 | return default(T); 74 | } 75 | return variable.CurrentValue; 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Source/Variables/Variable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5154ad7786f0143e9b74f72d145332d2 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Source/Variables/Vector2Reference.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace WolarGames.Variables 5 | { 6 | [Serializable] 7 | public class Vector2Reference : Reference 8 | { 9 | public Vector2Variable VariableReference; 10 | 11 | public override Variable Variable 12 | { 13 | get { 14 | return VariableReference; 15 | } 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Source/Variables/Vector2Reference.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b45ece1682d745d4ab71342b41c4fe37 3 | timeCreated: 1530649885 -------------------------------------------------------------------------------- /Source/Variables/Vector2Variable.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CreateAssetMenu(menuName = "Variables/Vector2", order = 70)] 6 | public class Vector2Variable : Variable 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/Vector2Variable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 113e170dbafe4a20b3b660ad032851d0 3 | timeCreated: 1530649947 -------------------------------------------------------------------------------- /Source/Variables/Vector3Reference.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace WolarGames.Variables 5 | { 6 | [Serializable] 7 | public class Vector3Reference : Reference 8 | { 9 | public Vector3Variable VariableReference; 10 | 11 | public override Variable Variable 12 | { 13 | get { 14 | return VariableReference; 15 | } 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Source/Variables/Vector3Reference.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c02333e96c9145589188f8ebe231df2f 3 | timeCreated: 1530649873 -------------------------------------------------------------------------------- /Source/Variables/Vector3Variable.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace WolarGames.Variables 4 | { 5 | [CreateAssetMenu(menuName = "Variables/Vector3", order = 71)] 6 | public class Vector3Variable : Variable 7 | { } 8 | } -------------------------------------------------------------------------------- /Source/Variables/Vector3Variable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b271f33f536045ff9a35d2047a12b1de 3 | timeCreated: 1530649937 -------------------------------------------------------------------------------- /Source/Views.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: bcefe4cccd914482ebb2711f1543b89a 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Source/Views/GameObjectToggle.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | #if REACTIVE_VARIABLE_RX_ENABLED 3 | using UniRx; 4 | #endif 5 | 6 | namespace WolarGames.Variables.Views 7 | { 8 | public class GameObjectToggle : MonoBehaviour 9 | { 10 | public BoolReference Variable; 11 | 12 | /// Should this be hidden when toggle is on 13 | public bool HideOnToggle; 14 | 15 | private void Awake() { 16 | 17 | #if REACTIVE_VARIABLE_RX_ENABLED 18 | Variable.AsObservable() 19 | .Subscribe(HandleVariableChange) 20 | .AddTo(this); 21 | #else 22 | Variable.OnValueChanged += HandleVariableChange; 23 | HandleVariableChange(Variable.Value); 24 | #endif 25 | } 26 | 27 | #if !REACTIVE_VARIABLE_RX_ENABLED 28 | private void OnDestroy() 29 | { 30 | Variable.OnValueChanged -= HandleVariableChange; 31 | } 32 | #endif 33 | 34 | private void HandleVariableChange(bool value) 35 | { 36 | gameObject.SetActive(!HideOnToggle && value || HideOnToggle && !value); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Source/Views/GameObjectToggle.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d88c4d0e00abd43bd8576c84c1b46dd8 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Source/Views/VariableToggle.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEngine.UI; 3 | #if REACTIVE_VARIABLE_RX_ENABLED 4 | using UniRx; 5 | #endif 6 | 7 | namespace WolarGames.Variables.Views 8 | { 9 | [RequireComponent(typeof(Toggle))] 10 | public class VariableToggle : MonoBehaviour 11 | { 12 | public BoolVariable Variable; 13 | 14 | private Toggle _toggle; 15 | 16 | void Start() { 17 | _toggle = GetComponent(); 18 | 19 | #if REACTIVE_VARIABLE_RX_ENABLED 20 | Variable.AsObservable() 21 | .Subscribe(HandleValueChanged) 22 | .AddTo(this); 23 | 24 | _toggle.OnValueChangedAsObservable() 25 | .Subscribe(HandleToggleValueChanged) 26 | .AddTo(this); 27 | #else 28 | HandleValueChanged(Variable.CurrentValue); 29 | _toggle.onValueChanged.AddListener(HandleToggleValueChanged); 30 | Variable.OnValueChanged += HandleValueChanged; 31 | #endif 32 | } 33 | 34 | #if !REACTIVE_VARIABLE_RX_ENABLED 35 | 36 | private void OnDestroy() 37 | { 38 | _toggle.onValueChanged.RemoveListener(HandleToggleValueChanged); 39 | Variable.OnValueChanged -= HandleValueChanged; 40 | } 41 | 42 | #endif 43 | 44 | private void HandleToggleValueChanged(bool value) 45 | { 46 | Variable.CurrentValue = value; 47 | } 48 | 49 | private void HandleValueChanged(bool value) 50 | { 51 | _toggle.isOn = value; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Source/Views/VariableToggle.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9eac1cc0946e24ccb8824aba4598ed89 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Templates.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 38b80d28a5df64f0588a4cd1f702ba48 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Templates/template_Editor_ReferenceDrawer.txt: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | using WolarGames.Variables; 3 | 4 | #NAMESPACE_KEYWORD# #NAMESPACE# 5 | #NAMESPACE_OPEN# 6 | [CustomPropertyDrawer(typeof(#VARIABLE#Reference))] 7 | public class #VARIABLE#ReferenceDrawer : ReferenceDrawer 8 | { } 9 | #NAMESPACE_CLOSE# -------------------------------------------------------------------------------- /Templates/template_Editor_ReferenceDrawer.txt.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e814725b323134487a8cbf259dfd4f3c 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Templates/template_Editor_VariableDrawer.txt: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | using WolarGames.Variables; 3 | 4 | #NAMESPACE_KEYWORD# #NAMESPACE# 5 | #NAMESPACE_OPEN# 6 | [CustomEditor(typeof(#VARIABLE#Variable))] 7 | public class #VARIABLE#VariableDrawer : VariableDrawer<#VARIABLE#> 8 | { } 9 | #NAMESPACE_CLOSE# -------------------------------------------------------------------------------- /Templates/template_Editor_VariableDrawer.txt.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: facf94621ccbe4bcc81ad1017c442a23 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Templates/template_Reference.txt: -------------------------------------------------------------------------------- 1 | using System; 2 | using WolarGames.Variables; 3 | 4 | #NAMESPACE_KEYWORD# #NAMESPACE# 5 | #NAMESPACE_OPEN# 6 | [Serializable] 7 | public class #VARIABLE#Reference : Reference<#VARIABLE#> 8 | { 9 | public #VARIABLE#Variable VariableReference; 10 | 11 | public override Variable<#VARIABLE#> Variable 12 | { 13 | get { 14 | return VariableReference; 15 | } 16 | } 17 | } 18 | #NAMESPACE_CLOSE# -------------------------------------------------------------------------------- /Templates/template_Reference.txt.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 53b6b50b9ed4d4fb492598b3cec61533 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Templates/template_Variable.txt: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using WolarGames.Variables; 3 | 4 | #NAMESPACE_KEYWORD# #NAMESPACE# 5 | #NAMESPACE_OPEN# 6 | [CreateAssetMenu(menuName = "Variables/#VARIABLE#", order = 100)] 7 | public class #VARIABLE#Variable : Variable<#VARIABLE#> 8 | { } 9 | #NAMESPACE_CLOSE# -------------------------------------------------------------------------------- /Templates/template_Variable.txt.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8d254cada9c8744fdbb7755872018ed2 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | --------------------------------------------------------------------------------