유니티의 Asset을 이름으로 얻고 싶은 거.
13644 단어 AssetBundleUnityC#
부가 또 떨어졌어요.
Play On Mac에서 예전의 Railload Tycoon2를 플레이하면 컴퓨터까지 떨어지는 경우가 많습니다.
그래도
아까 같이 결성한 유니티도 동반자로 떨어졌어요.
그 영향인지 부착이 완전히 떨어져 엉망진창이 되었다.
아무리 조심해도 부착이 떨어지는 경우가 있다.사람이요?
뭐 공부 해요?
예를 들어, Sprite와 Prefab을 많이 했다면
나는 이런 느낌으로 List로 추가할 것이라고 생각하지만, 나는 이것을 멈추고 싶다.
인덱스로 뭘 지정해도 치안이 안 좋아요.
이 근처에 어떻게 붙어있으면 QoL이 올라갈 것 같아요.
썼어
AutoAssetLoader.csusing System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
public class AutoAssetLoader<T> where T : UnityEngine.Object
{
private readonly Dictionary<string, T> _assets = new Dictionary<string, T>();
private string GetFilter()
{
if (typeof(T) == typeof(GameObject)) return "t:Prefab t:GameObject";
else if (typeof(T) == typeof(Sprite)) return "t:Sprite";
throw new Exception("とりあえずprefabとspriteのみ対応");
}
public AutoAssetLoader(string dir)
{
var assetPaths = AssetDatabase
.FindAssets(GetFilter(), new string[] { "Assets/Editor/" + dir } )
.Select(AssetDatabase.GUIDToAssetPath);
foreach (var path in assetPaths)
{
var asset = AssetDatabase.LoadAssetAtPath<T>(path);
_assets.Add(asset.name, asset);
}
}
public T Get(string name)
{
var f = _assets.TryGetValue(name, out var o);
return f ? o : null;
}
public IEnumerable<string> Names() => _assets.Keys;
}
#else
public class AutoAssetLoader<T> where T : UnityEngine.Object
{
private readonly AssetBundle _assetBundle;
private readonly Dictionary<string, T> _assets = new Dictionary<string, T>();
public AutoAssetLoader(string path)
{
_assetBundle = AssetBundle.LoadFromFile("AssetBundleData/" + path);
}
public T Get(string name)
{
var f = _assets.TryGetValue(name, out var asset);
if (f) return asset;
asset = _assetBundle.LoadAsset<T>(name);
_assets.Add(name, asset);
return asset;
}
public IEnumerable<string> Names() => _assetBundle.GetAllAssetNames().Select(str=>
{
var from = str.LastIndexOf("/", StringComparison.Ordinal) + "/".Length;
var to = str.LastIndexOf(".", StringComparison.Ordinal);
var len = to < from ? (str.Length - from) : (to - from);
return str.Substring(from, len);
});
}
#endif
디렉터리의prefab와sprite 등 모든 내용을 읽습니다.
AssetDatabase는 UnityEditor 이름 공간에 있습니다. 즉, 구축할 때 사용할 수 없습니다.
사전 프로세서 명령을 사용하여 전환합니다.
개발할 때 편집기 폴더에 Prefab 또는 Sprite를 배치합니다.
나올 때 Asset Bundle을 잘 만들어야 돼요.
루트 디렉토리 바로 아래에 있는 AssetBundleData에 놓습니다.
UnityProject/
├ Assets/
│└ Editor/
│ ├ Prefabs/
│ └ Sprites/
└ AssetBundleData/
사용 방법
usage.cs AutoAssetLoader<GameObject> prefabs = new AutoAssetLoader<GameObject>("Prefabs");
foreach (var name in prefabs.Names())
{
Instantiate(prefabs.Get(name));
}
AutoAssetLoader<Sprite> sprites = new AutoAssetLoader<Sprite>("Sprites");
image.sprite = sprites.Get("Cat");
보충하여 기록하다
2019/9/22
AssetDatabase를 구축할 때 사용할 수 없음을 발견하여 AssetBundle로 전환하기로 결정했습니다.
Reference
이 문제에 관하여(유니티의 Asset을 이름으로 얻고 싶은 거.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/__poosuke/items/3242d3f26a233fdee02a
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
예를 들어, Sprite와 Prefab을 많이 했다면

나는 이런 느낌으로 List로 추가할 것이라고 생각하지만, 나는 이것을 멈추고 싶다.
인덱스로 뭘 지정해도 치안이 안 좋아요.
이 근처에 어떻게 붙어있으면 QoL이 올라갈 것 같아요.
썼어
AutoAssetLoader.csusing System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
public class AutoAssetLoader<T> where T : UnityEngine.Object
{
private readonly Dictionary<string, T> _assets = new Dictionary<string, T>();
private string GetFilter()
{
if (typeof(T) == typeof(GameObject)) return "t:Prefab t:GameObject";
else if (typeof(T) == typeof(Sprite)) return "t:Sprite";
throw new Exception("とりあえずprefabとspriteのみ対応");
}
public AutoAssetLoader(string dir)
{
var assetPaths = AssetDatabase
.FindAssets(GetFilter(), new string[] { "Assets/Editor/" + dir } )
.Select(AssetDatabase.GUIDToAssetPath);
foreach (var path in assetPaths)
{
var asset = AssetDatabase.LoadAssetAtPath<T>(path);
_assets.Add(asset.name, asset);
}
}
public T Get(string name)
{
var f = _assets.TryGetValue(name, out var o);
return f ? o : null;
}
public IEnumerable<string> Names() => _assets.Keys;
}
#else
public class AutoAssetLoader<T> where T : UnityEngine.Object
{
private readonly AssetBundle _assetBundle;
private readonly Dictionary<string, T> _assets = new Dictionary<string, T>();
public AutoAssetLoader(string path)
{
_assetBundle = AssetBundle.LoadFromFile("AssetBundleData/" + path);
}
public T Get(string name)
{
var f = _assets.TryGetValue(name, out var asset);
if (f) return asset;
asset = _assetBundle.LoadAsset<T>(name);
_assets.Add(name, asset);
return asset;
}
public IEnumerable<string> Names() => _assetBundle.GetAllAssetNames().Select(str=>
{
var from = str.LastIndexOf("/", StringComparison.Ordinal) + "/".Length;
var to = str.LastIndexOf(".", StringComparison.Ordinal);
var len = to < from ? (str.Length - from) : (to - from);
return str.Substring(from, len);
});
}
#endif
디렉터리의prefab와sprite 등 모든 내용을 읽습니다.
AssetDatabase는 UnityEditor 이름 공간에 있습니다. 즉, 구축할 때 사용할 수 없습니다.
사전 프로세서 명령을 사용하여 전환합니다.
개발할 때 편집기 폴더에 Prefab 또는 Sprite를 배치합니다.
나올 때 Asset Bundle을 잘 만들어야 돼요.
루트 디렉토리 바로 아래에 있는 AssetBundleData에 놓습니다.
UnityProject/
├ Assets/
│└ Editor/
│ ├ Prefabs/
│ └ Sprites/
└ AssetBundleData/
사용 방법
usage.cs AutoAssetLoader<GameObject> prefabs = new AutoAssetLoader<GameObject>("Prefabs");
foreach (var name in prefabs.Names())
{
Instantiate(prefabs.Get(name));
}
AutoAssetLoader<Sprite> sprites = new AutoAssetLoader<Sprite>("Sprites");
image.sprite = sprites.Get("Cat");
보충하여 기록하다
2019/9/22
AssetDatabase를 구축할 때 사용할 수 없음을 발견하여 AssetBundle로 전환하기로 결정했습니다.
Reference
이 문제에 관하여(유니티의 Asset을 이름으로 얻고 싶은 거.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/__poosuke/items/3242d3f26a233fdee02a
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
public class AutoAssetLoader<T> where T : UnityEngine.Object
{
private readonly Dictionary<string, T> _assets = new Dictionary<string, T>();
private string GetFilter()
{
if (typeof(T) == typeof(GameObject)) return "t:Prefab t:GameObject";
else if (typeof(T) == typeof(Sprite)) return "t:Sprite";
throw new Exception("とりあえずprefabとspriteのみ対応");
}
public AutoAssetLoader(string dir)
{
var assetPaths = AssetDatabase
.FindAssets(GetFilter(), new string[] { "Assets/Editor/" + dir } )
.Select(AssetDatabase.GUIDToAssetPath);
foreach (var path in assetPaths)
{
var asset = AssetDatabase.LoadAssetAtPath<T>(path);
_assets.Add(asset.name, asset);
}
}
public T Get(string name)
{
var f = _assets.TryGetValue(name, out var o);
return f ? o : null;
}
public IEnumerable<string> Names() => _assets.Keys;
}
#else
public class AutoAssetLoader<T> where T : UnityEngine.Object
{
private readonly AssetBundle _assetBundle;
private readonly Dictionary<string, T> _assets = new Dictionary<string, T>();
public AutoAssetLoader(string path)
{
_assetBundle = AssetBundle.LoadFromFile("AssetBundleData/" + path);
}
public T Get(string name)
{
var f = _assets.TryGetValue(name, out var asset);
if (f) return asset;
asset = _assetBundle.LoadAsset<T>(name);
_assets.Add(name, asset);
return asset;
}
public IEnumerable<string> Names() => _assetBundle.GetAllAssetNames().Select(str=>
{
var from = str.LastIndexOf("/", StringComparison.Ordinal) + "/".Length;
var to = str.LastIndexOf(".", StringComparison.Ordinal);
var len = to < from ? (str.Length - from) : (to - from);
return str.Substring(from, len);
});
}
#endif
usage.cs
AutoAssetLoader<GameObject> prefabs = new AutoAssetLoader<GameObject>("Prefabs");
foreach (var name in prefabs.Names())
{
Instantiate(prefabs.Get(name));
}
AutoAssetLoader<Sprite> sprites = new AutoAssetLoader<Sprite>("Sprites");
image.sprite = sprites.Get("Cat");
보충하여 기록하다
2019/9/22
AssetDatabase를 구축할 때 사용할 수 없음을 발견하여 AssetBundle로 전환하기로 결정했습니다.
Reference
이 문제에 관하여(유니티의 Asset을 이름으로 얻고 싶은 거.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/__poosuke/items/3242d3f26a233fdee02a
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(유니티의 Asset을 이름으로 얻고 싶은 거.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/__poosuke/items/3242d3f26a233fdee02a텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)