Skip to content

Instantly share code, notes, and snippets.

@enue
Last active December 23, 2016 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enue/6b244657815a671bf899819171709311 to your computer and use it in GitHub Desktop.
Save enue/6b244657815a671bf899819171709311 to your computer and use it in GitHub Desktop.
[Unity] Resourcesロードのパス指定を自動化する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Unity 5.5.0p3
namespace TSKT
{
public class ResourceReferer<T> : ScriptableObject
where T : Object
{
[SerializeField]
string path;
public ResourceLoader<T> Load()
{
return new ResourceLoader<T>(path);
}
[SerializeField]
T asset;
[HideInInspector]
[SerializeField]
string guid;
#if UNITY_EDITOR
void OnValidate()
{
string fullPath = null;
if (asset)
{
fullPath = UnityEditor.AssetDatabase.GetAssetPath(asset);
guid = UnityEditor.AssetDatabase.AssetPathToGUID(fullPath);
}
else if (!string.IsNullOrEmpty(guid))
{
fullPath = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
}
if (!string.IsNullOrEmpty(fullPath))
{
var i = fullPath.IndexOf("/Resources/");
if (i < 0)
{
UnityEngine.Assertions.Assert.IsTrue(false, "asset has to be in Resources : " + fullPath);
}
else
{
asset = null;
var resourcePath = fullPath.Remove(0, i + "/Resources/".Length);
var dir = System.IO.Path.GetDirectoryName(resourcePath);
var file = System.IO.Path.GetFileNameWithoutExtension(resourcePath);
var newPath = dir + "/" + file;
if (path != newPath)
{
path = newPath;
UnityEditor.EditorUtility.SetDirty(this);
}
}
}
}
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment