summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/UMotion/UMotionEditor/Scripts/Application/AssetResourcesFile.cs
blob: 7f5392bed913f0169a02cbfb6ebbe4f79cab142b (plain)
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
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
using System.IO;

namespace UMotionEditor
{
    public class AssetResourcesFile : ScriptableObject
    {
        //********************************************************************************
        // Public Properties
        //********************************************************************************

        //********************************************************************************
        // Private Properties
        //********************************************************************************

        #pragma warning disable 0649 // Suppress "Field 'field' is never assigned to, and will always have its default value 'value'"
        [Serializable]
        private struct ResourceDefinition
        {
            public string Name;
            public UnityEngine.Object Reference;
        }
        #pragma warning restore 0649

        //----------------------
        // Inspector
        //----------------------
        [SerializeField]private List<ResourceDefinition> resourcesList = new List<ResourceDefinition>();
        [SerializeField]private List<ResourceDefinition> optionalResourcesList = new List<ResourceDefinition>();
        private Dictionary<string, UnityEngine.Object> resourcesDictionary = new Dictionary<string, UnityEngine.Object>();

        //----------------------
        // Internal
        //----------------------

        //********************************************************************************
        // Public Methods
        //********************************************************************************

        public static AssetResourcesFile FindAssetResourcesFile()
        {
            string[] resourceFilesGUID = AssetDatabase.FindAssets("UMotionResources t:AssetResourcesFile");

            if (resourceFilesGUID.Length > 1)
            {
                throw new UnityException("More than one resource file was found. Please remove all UMotion files and install UMotion again.");
            }
            else if (resourceFilesGUID.Length == 0)
            {
                throw new UnityException("Resource file not found. Please install UMotion again.");
            }
            else
            {
                AssetResourcesFile resourceFile = AssetDatabase.LoadAssetAtPath<AssetResourcesFile>(AssetDatabase.GUIDToAssetPath(resourceFilesGUID[0]));

                resourceFile.InitializeDictionary();

                return resourceFile;
            }
        }

        public string GetEditorDataPath()
        {
            string resourcesPath = AssetDatabase.GetAssetPath(this);

            string dataPath = Path.GetDirectoryName(resourcesPath);
            dataPath = Path.Combine(Path.GetDirectoryName(dataPath), "Data");
            
            return dataPath;
        }

        public T GetResource<T>(string name, bool required = true) where T : UnityEngine.Object
        {
            T loadedObject = null;
            UnityEngine.Object obj;
            if (resourcesDictionary.TryGetValue(name, out obj))
            {
                loadedObject = obj as T;
            }

            if (required && (loadedObject == null))
            {
                throw new Exception(string.Format("Resource \"{0}\" can not be loaded.", name));
            }
            else
            {
                return loadedObject;
            }
        }

        //********************************************************************************
        // Private Methods
        //********************************************************************************

        private void InitializeDictionary()
        {
            resourcesDictionary.Clear();
            foreach (ResourceDefinition resourceDef in resourcesList)
            {
                if (resourceDef.Reference == null)
                {
                    throw new UnityException(string.Format("Required resource \"{0}\" not found. Please reinstall UMotion.", resourceDef.Name));
                }
                else
                {
                    resourcesDictionary.Add(resourceDef.Name, resourceDef.Reference);
                }
            }
            foreach (ResourceDefinition resourceDef in optionalResourcesList)
            {
                resourcesDictionary.Add(resourceDef.Name, resourceDef.Reference);
            }
        }
    }
}
#endif