File size: 6,368 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | #if UNITY_EDITOR
using System;
using System.IO;
using UnityEditor;
////TODO: ensure that GUIDs in the asset are unique
namespace UnityEngine.InputSystem.Editor
{
/// <summary>
/// Keeps a reference to the asset being edited and maintains a copy of the asset object
/// around for editing.
/// </summary>
[Serializable]
internal class InputActionAssetManager : IDisposable
{
[SerializeField] internal InputActionAsset m_AssetObjectForEditing;
[SerializeField] private InputActionAsset m_ImportedAssetObject;
[SerializeField] private string m_AssetGUID;
[SerializeField] private string m_ImportedAssetJson;
[SerializeField] private bool m_IsDirty;
private SerializedObject m_SerializedObject;
public string guid => m_AssetGUID;
public string path
{
get
{
Debug.Assert(!string.IsNullOrEmpty(m_AssetGUID), "Asset GUID is empty");
return AssetDatabase.GUIDToAssetPath(m_AssetGUID);
}
}
public string name
{
get
{
if (m_ImportedAssetObject != null)
return m_ImportedAssetObject.name;
if (!string.IsNullOrEmpty(path))
return Path.GetFileNameWithoutExtension(path);
return string.Empty;
}
}
private InputActionAsset importedAsset
{
get
{
if (m_ImportedAssetObject == null)
LoadImportedObjectFromGuid();
return m_ImportedAssetObject;
}
}
public Action<bool> onDirtyChanged { get; set; }
public InputActionAssetManager(InputActionAsset inputActionAsset)
{
m_ImportedAssetObject = inputActionAsset;
bool isGUIDObtained = AssetDatabase.TryGetGUIDAndLocalFileIdentifier(importedAsset, out m_AssetGUID, out long _);
Debug.Assert(isGUIDObtained, $"Failed to get asset {inputActionAsset.name} GUID");
}
public SerializedObject serializedObject => m_SerializedObject;
public bool dirty => m_IsDirty;
public bool Initialize()
{
if (m_AssetObjectForEditing == null)
{
if (importedAsset == null)
// The asset we want to edit no longer exists.
return false;
CreateWorkingCopyAsset();
}
else
{
m_SerializedObject = new SerializedObject(m_AssetObjectForEditing);
}
return true;
}
public void Dispose()
{
m_SerializedObject?.Dispose();
}
public bool ReInitializeIfAssetHasChanged()
{
var asset = importedAsset;
var json = asset.ToJson();
if (m_ImportedAssetJson == json)
return false;
CreateWorkingCopyAsset();
return true;
}
private void CreateWorkingCopyAsset()
{
if (m_AssetObjectForEditing != null)
Cleanup();
// Duplicate the asset along 1:1. Unlike calling Clone(), this will also preserve
// GUIDs.
var asset = importedAsset;
m_AssetObjectForEditing = Object.Instantiate(asset);
m_AssetObjectForEditing.hideFlags = HideFlags.HideAndDontSave;
m_AssetObjectForEditing.name = importedAsset.name;
m_ImportedAssetJson = asset.ToJson();
m_SerializedObject = new SerializedObject(m_AssetObjectForEditing);
}
public void Cleanup()
{
if (m_AssetObjectForEditing == null)
return;
Object.DestroyImmediate(m_AssetObjectForEditing);
m_AssetObjectForEditing = null;
}
public void LoadImportedObjectFromGuid()
{
// https://fogbugz.unity3d.com/f/cases/1313185/
// InputActionEditorWindow being an EditorWindow, it will be saved as part of the editor's
// window layout. When a project is opened that has no Library/ folder, the layout from the
// most recently opened project is used. Which means that when opening an .inputactions
// asset in project A, then closing it, and then opening project B, restoring the window layout
// also tries to restore the InputActionEditorWindow having that very same asset open -- which
// will lead nowhere except there happens to be an InputActionAsset with the very same GUID in
// the project.
var assetPath = path;
if (!string.IsNullOrEmpty(assetPath))
m_ImportedAssetObject = AssetDatabase.LoadAssetAtPath<InputActionAsset>(assetPath);
}
public void ApplyChanges()
{
m_SerializedObject.ApplyModifiedProperties();
m_SerializedObject.Update();
}
internal void SaveChangesToAsset()
{
Debug.Assert(importedAsset != null);
// Update JSON.
var asset = m_AssetObjectForEditing;
m_ImportedAssetJson = asset.ToJson();
// Write out, if changed.
var assetPath = path;
var existingJson = File.ReadAllText(assetPath);
if (m_ImportedAssetJson != existingJson)
{
EditorHelpers.CheckOut(assetPath);
File.WriteAllText(assetPath, m_ImportedAssetJson);
AssetDatabase.ImportAsset(assetPath);
}
m_IsDirty = false;
onDirtyChanged(false);
}
public void SetAssetDirty()
{
m_IsDirty = true;
onDirtyChanged(true);
}
public bool ImportedAssetObjectEquals(InputActionAsset inputActionAsset)
{
if (importedAsset == null)
return false;
return importedAsset.Equals(inputActionAsset);
}
public void UpdateAssetDirtyState()
{
m_SerializedObject.Update();
m_IsDirty = m_AssetObjectForEditing.ToJson() != importedAsset.ToJson();
onDirtyChanged(m_IsDirty);
}
}
}
#endif // UNITY_EDITOR
|