1+ using System ;
2+ using System . Collections ;
3+ using System . Collections . Generic ;
4+ using System . Linq ;
5+ using UnityEditor ;
6+ using UnityEngine ;
7+
8+ namespace Urho3DExporter
9+ {
10+ public class AssetCollection : IEnumerable < AssetContext >
11+ {
12+ private readonly string _urhoDataPath ;
13+ private readonly List < AssetContext > _assets ;
14+
15+ public AssetCollection ( string urhoDataPath , IEnumerable < AssetContext > assets )
16+ {
17+ _urhoDataPath = urhoDataPath . Replace ( '/' , '\\ ' ) ;
18+ if ( ! _urhoDataPath . EndsWith ( "\\ " ) )
19+ _urhoDataPath += '\\ ' ;
20+ _assets = assets . ToList ( ) ;
21+ foreach ( var assetContext in assets . Where ( _=> _ . Type == typeof ( Material ) ) )
22+ {
23+ AddMaterialPath ( AssetDatabase . LoadAssetAtPath < Material > ( assetContext . AssetPath ) ,
24+ assetContext . UrhoAssetName ) ;
25+ }
26+ }
27+
28+ public IEnumerator < AssetContext > GetEnumerator ( )
29+ {
30+ return _assets . GetEnumerator ( ) ;
31+ }
32+
33+ IEnumerator IEnumerable . GetEnumerator ( )
34+ {
35+ return ( ( IEnumerable ) _assets ) . GetEnumerator ( ) ;
36+ }
37+
38+ public void AddMeshPath ( Mesh mesh , string fileName )
39+ {
40+ if ( fileName . StartsWith ( _urhoDataPath , StringComparison . InvariantCultureIgnoreCase ) )
41+ fileName = fileName . Substring ( _urhoDataPath . Length ) . Replace ( '\\ ' , '/' ) ;
42+ TryAdd ( _meshPaths , mesh , mesh . name , fileName ) ;
43+ }
44+
45+ public bool TryAdd ( Dictionary < string , string > values , UnityEngine . Object asset , string name , string fileName )
46+ {
47+ var path = AssetDatabase . GetAssetPath ( asset ) ;
48+ var id = path + "#" + name ;
49+ if ( values . ContainsKey ( id ) )
50+ {
51+ //Debug.LogError("Duplicate asset " + id);
52+ return false ;
53+ }
54+ values . Add ( id , fileName ) ;
55+ return true ;
56+ }
57+
58+ Dictionary < string , string > _meshPaths = new Dictionary < string , string > ( ) ;
59+
60+ public bool TryGetMeshPath ( Mesh sharedMesh , out string meshPath )
61+ {
62+ meshPath = null ;
63+ if ( sharedMesh == null )
64+ return false ;
65+ var path = AssetDatabase . GetAssetPath ( sharedMesh ) ;
66+ var id = path + "#" + sharedMesh . name ;
67+ return _meshPaths . TryGetValue ( id , out meshPath ) ;
68+ }
69+
70+ public void AddMaterialPath ( Material material , string fileName )
71+ {
72+
73+ if ( fileName . StartsWith ( _urhoDataPath , StringComparison . InvariantCultureIgnoreCase ) )
74+ fileName = fileName . Substring ( _urhoDataPath . Length ) . Replace ( '\\ ' , '/' ) ;
75+ TryAdd ( _materialPaths , material , material . name , fileName ) ;
76+ }
77+
78+ Dictionary < string , string > _materialPaths = new Dictionary < string , string > ( ) ;
79+
80+ public bool TryGetMaterialPath ( Material sharedMaterial , out string materialPath )
81+ {
82+ materialPath = null ;
83+ if ( sharedMaterial == null )
84+ return false ;
85+ var path = AssetDatabase . GetAssetPath ( sharedMaterial ) ;
86+ var id = path + "#" + sharedMaterial . name ;
87+ return _materialPaths . TryGetValue ( id , out materialPath ) ;
88+ }
89+
90+
91+
92+ public void AddTexturePath ( Texture material , string fileName )
93+ {
94+
95+ if ( fileName . StartsWith ( _urhoDataPath , StringComparison . InvariantCultureIgnoreCase ) )
96+ fileName = fileName . Substring ( _urhoDataPath . Length ) . Replace ( '\\ ' , '/' ) ;
97+ TryAdd ( _texturePaths , material , material . name , fileName ) ;
98+ }
99+
100+ Dictionary < string , string > _texturePaths = new Dictionary < string , string > ( ) ;
101+
102+ public bool TryGetTexturePath ( Texture sharedTexture , out string materialPath )
103+ {
104+ materialPath = null ;
105+ if ( sharedTexture == null )
106+ return false ;
107+ var path = AssetDatabase . GetAssetPath ( sharedTexture ) ;
108+ var id = path + "#" + sharedTexture . name ;
109+ return _texturePaths . TryGetValue ( id , out materialPath ) ;
110+ }
111+ }
112+ }
0 commit comments