Skip to content

Commit 08857da

Browse files
committed
Initial release
1 parent 29091fd commit 08857da

19 files changed

+2155
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.meta
File renamed without changes.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
namespace Urho3DExporter
8+
{
9+
public class AssetContext
10+
{
11+
public string UrhoFileName { get; private set; }
12+
13+
public string UrhoAssetName { get; private set; }
14+
15+
public string FullPath { get; private set; }
16+
17+
public string RelPath { get; private set; }
18+
19+
public Type Type { get; private set; }
20+
21+
public string AssetPath { get; private set; }
22+
23+
public string Guid { get; private set; }
24+
25+
public string ContentFolder { get; private set; }
26+
27+
public bool Is3DAsset { get; private set; }
28+
29+
private static readonly HashSet<string> _supported3DFormats = new HashSet<string>()
30+
{
31+
".fbx",
32+
".obj",
33+
".dae",
34+
".3ds",
35+
".dxf",
36+
".max",
37+
".ma",
38+
".mb",
39+
".blend",
40+
};
41+
42+
public static AssetContext Create(string guid, string urhoDataFolder)
43+
{
44+
var res = new AssetContext
45+
{
46+
Guid = guid
47+
};
48+
res.AssetPath = AssetDatabase.GUIDToAssetPath(guid);
49+
if (!string.IsNullOrEmpty(res.AssetPath))
50+
{
51+
res.Type = AssetDatabase.GetMainAssetTypeAtPath(res.AssetPath);
52+
res.RelPath = res.AssetPath;
53+
if (res.RelPath.StartsWith("Assets/", StringComparison.InvariantCultureIgnoreCase))
54+
res.RelPath = res.RelPath.Substring("Assets/".Length);
55+
res.FullPath = Path.Combine(Application.dataPath, res.RelPath);
56+
res.FileExtension = System.IO.Path.GetExtension(res.AssetPath).ToLower();
57+
res.UrhoAssetName = res.RelPath;
58+
if (res.Type == typeof(Material))
59+
res.UrhoAssetName = RepaceExtension(res.UrhoAssetName, ".xml");
60+
else if (res.Type == typeof(GameObject))
61+
{
62+
res.UrhoAssetName = RepaceExtension(res.UrhoAssetName, ".xml");
63+
res.Is3DAsset = _supported3DFormats.Contains(res.FileExtension);
64+
}
65+
else if (res.Type == typeof(SceneAsset)) res.UrhoAssetName = RepaceExtension(res.UrhoAssetName, ".xml");
66+
res.UrhoFileName = System.IO.Path.Combine(urhoDataFolder, res.UrhoAssetName);
67+
if (res.Is3DAsset)
68+
{
69+
res.ContentFolder = res.UrhoFileName.Substring(0, res.UrhoFileName.Length - ".xml".Length);
70+
}
71+
}
72+
73+
return res;
74+
}
75+
76+
public string FileExtension { get; private set; }
77+
78+
private static string RepaceExtension(string resUrhoAssetName, string newExt)
79+
{
80+
var ext = System.IO.Path.GetExtension(resUrhoAssetName);
81+
return resUrhoAssetName.Substring(0, resUrhoAssetName.Length - ext.Length) + newExt;
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)