static bool SetAndLoadIfNull(System.Object container, FieldInfo info, string path, ReloadAttribute.Package location) { if (IsNull(container, info)) { info.SetValue(container, Load(path, info.FieldType, location)); return(true); } return(false); }
static bool SetAndLoadIfNull(Array array, int index, string path, ReloadAttribute.Package location) { var element = array.GetValue(index); if (IsNull(element)) { array.SetValue(Load(path, array.GetType().GetElementType(), location), index); return(true); } return(false); }
static UnityEngine.Object Load(string path, Type type, ReloadAttribute.Package location) { // Check if asset exist. // Direct loading can be prevented by AssetDatabase being reloading. var guid = AssetDatabase.AssetPathToGUID(path); if (location == ReloadAttribute.Package.Root && String.IsNullOrEmpty(guid)) { throw new Exception($"Cannot load. Incorrect path: {path}"); } // Else the path is good. Attempt loading resource if AssetDatabase available. UnityEngine.Object result; switch (location) { case ReloadAttribute.Package.Builtin: if (type == typeof(Shader)) { result = Shader.Find(path); } else { result = Resources.GetBuiltinResource(type, path); //handle wrong path error } break; case ReloadAttribute.Package.BuiltinExtra: if (type == typeof(Shader)) { result = Shader.Find(path); } else { result = AssetDatabase.GetBuiltinExtraResource(type, path); //handle wrong path error } break; case ReloadAttribute.Package.Root: result = AssetDatabase.LoadAssetAtPath(path, type); break; default: throw new NotImplementedException($"Unknown {location}"); } if (IsNull(result)) { var e = new Exception($"Cannot load. Path {path} is correct but AssetDatabase cannot load now."); e.Data["InvalidImport"] = 1; throw e; } return(result); }