public Material Clone(AssetCabinet file, bool cloneTextures) { Component mat = file != this.file ? file.Bundle != null && !file.Bundle.m_IsStreamedSceneAssetBundle ? file.Bundle.FindComponent(m_Name, UnityClassID.Material) : file.Components.Find ( delegate(Component asset) { return(asset.classID() == UnityClassID.Material && (asset is NotLoaded ? ((NotLoaded)asset).Name : ((Material)asset).m_Name) == m_Name); } ) : null; if (mat == null) { file.MergeTypeDefinition(this.file, UnityClassID.Material); mat = new Material(file); if (file.Bundle != null && this.file.Bundle != null) { List <Component> externals = new List <Component>(); AssetBundle.AddExternalAssetsFromPreloadTable(this, this.file.Bundle, externals); for (int i = 0; i < externals.Count; i++) { ExternalAsset ext = (ExternalAsset)externals[i]; string assetPath = this.file.References[ext.FileID - 1].assetPath; int cabPos = assetPath.LastIndexOf("/") + 1; if (!AssetCabinet.LoadedCabinets.ContainsKey(assetPath.Substring(cabPos))) { ext.FileID = file.MergeReference(this.file, ext.FileID); } else { externals.RemoveAt(i--); } } file.Bundle.AddComponent(m_Name, mat, externals); } CopyTo((Material)mat, cloneTextures); } else if (mat is NotLoaded) { NotLoaded notLoaded = (NotLoaded)mat; if (notLoaded.replacement != null) { mat = notLoaded.replacement; } else { mat = file.LoadComponent(file.SourceStream, notLoaded); } } return((Material)mat); }
private static void AddExternalAsset(List <Component> assets, PPtr <Shader> dep) { Component found = assets.Find ( delegate(Component c) { if (c is ExternalAsset) { ExternalAsset e = (ExternalAsset)c; return(e.FileID == dep.m_FileID && e.PathID == dep.m_PathID); } return(false); } ); if (found == null) { ExternalAsset extShader = new ExternalAsset(); extShader.FileID = dep.m_FileID; extShader.PathID = dep.m_PathID; assets.Add(extShader); } }
private static void GetDependantAssets(Component asset, List <Component> assets, List <Component> transforms, List <Component> containerRelated) { if (asset != null && !assets.Contains(asset)) { assets.Add(asset); switch (asset.classID1) { case UnityClassID.Animator: Animator animator = (Animator)asset; GetDependantAssets(animator.m_Avatar.asset, assets, transforms, containerRelated); GetDependantAssets(animator.m_Controller.asset, assets, transforms, containerRelated); foreach (Component ren in containerRelated) { GetDependantAssets(ren, assets, null, null); if (ren is MeshRenderer) { MeshRenderer meshR = (MeshRenderer)ren; PPtr <Mesh> meshPtr = Operations.GetMeshPtr(meshR); if (meshPtr != null) { if (meshPtr.m_FileID != 0) { Component found = assets.Find ( delegate(Component c) { if (c is ExternalAsset) { ExternalAsset e = (ExternalAsset)c; return(e.FileID == meshPtr.m_FileID && e.PathID == meshPtr.m_PathID); } return(false); } ); if (found == null) { ExternalAsset extMesh = new ExternalAsset(); extMesh.FileID = meshPtr.m_FileID; extMesh.PathID = meshPtr.m_PathID; assets.Add(extMesh); } } else if (meshPtr.instance != null && !assets.Contains(meshPtr.instance)) { assets.Add(meshPtr.instance); } } foreach (PPtr <Material> matPtr in meshR.m_Materials) { if (!assets.Contains(matPtr.asset)) { GetDependantAssets(matPtr.asset, assets, null, null); } } } } assets.Sort ( delegate(Component c1, Component c2) { if (c1 is ExternalAsset) { ExternalAsset e1 = (ExternalAsset)c1; if (c2 is ExternalAsset) { ExternalAsset e2 = (ExternalAsset)c2; return(e1.FileID != e2.FileID ? e1.FileID.CompareTo(e2.FileID) : e1.PathID.CompareTo(e2.PathID)); } return(-1); } else if (c2 is ExternalAsset) { return(1); } return(c1.pathID.CompareTo(c2.pathID)); } ); break; case UnityClassID.Avatar: break; case UnityClassID.AnimatorController: AnimatorController aCon = (AnimatorController)asset; for (int i = 0; i < aCon.m_AnimationClips.Count; i++) { assets.Add(aCon.m_AnimationClips[i].asset); containerRelated.Add(aCon.m_AnimationClips[i].asset); } break; case UnityClassID.AnimationClip: break; case UnityClassID.GameObject: GameObject gameObj = (GameObject)asset; animator = null; foreach (var compPair in gameObj.m_Component) { switch (compPair.Key) { case UnityClassID.Transform: Transform trans = (Transform)compPair.Value.instance; transforms.Add(trans); foreach (Transform child in trans) { GetDependantAssets(child.m_GameObject.asset, assets, transforms, containerRelated); } break; case UnityClassID.Animator: animator = (Animator)compPair.Value.asset; break; case UnityClassID.MeshRenderer: case UnityClassID.MeshFilter: case UnityClassID.SkinnedMeshRenderer: containerRelated.Add(compPair.Value.asset); break; case UnityClassID.MonoBehaviour: GetDependantAssets(compPair.Value.asset, assets, transforms, containerRelated); break; default: if (!assets.Contains(compPair.Value.asset)) { assets.Add(compPair.Value.asset); } break; } } if (animator != null) { foreach (Component trans in transforms) { GetDependantAssets(trans, assets, null, null); } GetDependantAssets(animator, assets, transforms, containerRelated); } break; case UnityClassID.Light: break; default: if (asset.classID2 == UnityClassID.MonoBehaviour) { MonoBehaviour monoB = (MonoBehaviour)asset; if (!assets.Contains(monoB.m_MonoScript.asset)) { assets.Add(monoB.m_MonoScript.asset); } } break; case UnityClassID.MonoScript: break; case UnityClassID.Transform: break; case UnityClassID.MeshRenderer: case UnityClassID.MeshFilter: case UnityClassID.SkinnedMeshRenderer: break; case UnityClassID.Material: Material mat = (Material)asset; foreach (var texVal in mat.m_SavedProperties.m_TexEnvs) { GetDependantAssets(texVal.Value.m_Texture.asset, assets, transforms, containerRelated); } if (mat.m_Shader.m_FileID != 0) { AddExternalAsset(assets, mat.m_Shader); } else if (mat.m_Shader.instance != null) { GetDependantAssets(mat.m_Shader.asset, assets, transforms, containerRelated); } break; case UnityClassID.Shader: Shader shader = (Shader)asset; foreach (PPtr <Shader> dep in shader.m_Dependencies) { if (dep.m_FileID != 0) { AddExternalAsset(assets, dep); } else if (dep.asset != null) { GetDependantAssets(dep.asset, assets, transforms, containerRelated); } } break; case UnityClassID.Sprite: assets.Remove(asset); Sprite sprite = (Sprite)asset; assets.Add(sprite.m_RD.texture.asset); assets.Add(sprite); break; case UnityClassID.Texture2D: break; } } }
private void UpdateComponent(Component asset, List <Tuple <List <PPtr <Object> >, List <KeyValuePair <string, AssetInfo> > > > containerGroups) { List <Component> assets = new List <Component>(); List <Component> transforms = new List <Component>(); List <Component> containerRelated = new List <Component>(); GetDependantAssets(asset, assets, transforms, containerRelated); if (asset.classID1 == UnityClassID.GameObject) { GameObject gameObj = (GameObject)asset; Animator animator = gameObj.FindLinkedComponent(UnityClassID.Animator); if (animator == null) { foreach (Component trans in transforms) { GetDependantAssets(trans, assets, null, null); } animator = new Animator(null, 0, UnityClassID.Animator, UnityClassID.Animator); animator.m_Avatar = new PPtr <Avatar>((Component)null); animator.m_Controller = new PPtr <RuntimeAnimatorController>((Component)null); GetDependantAssets(animator, assets, transforms, containerRelated); assets.Remove(animator); } } foreach (var group in containerGroups) { var preloadPart = group.Item1; var containerEntries = group.Item2; for (int i = 0; i < containerEntries.Count; i++) { var assetPair = containerEntries[i]; if (assetPair.Value.asset.asset == asset) { preloadPart.Clear(); for (int j = 0; j < assets.Count; j++) { if (assets[j] is ExternalAsset) { ExternalAsset extAsset = (ExternalAsset)assets[j]; PPtr <Object> preloadEntry = new PPtr <Object>((Component)null); preloadEntry.m_FileID = extAsset.FileID; preloadEntry.m_PathID = extAsset.PathID; preloadPart.Add(preloadEntry); } else { preloadPart.Add(new PPtr <Object>(assets[j])); } } string groupName = containerEntries[0].Key; string assetName = AssetCabinet.ToString(asset); if (String.Compare(groupName, assetName, true) != 0) { groupName = assetName.ToLower(); } if (containerEntries.Count > 1) { for (int j = 0; j < containerEntries.Count; j++) { switch (containerEntries[j].Value.asset.asset.classID1) { case UnityClassID.Mesh: case UnityClassID.AnimationClip: containerEntries.RemoveAt(j); j--; break; } } for (int j = containerRelated.Count - 1; j >= 0; j--) { AnimationClip clip = containerRelated[j] as AnimationClip; if (clip != null) { AssetInfo info = new AssetInfo(file); info.asset = new PPtr <Object>(clip); containerEntries.Insert(1, new KeyValuePair <string, AssetInfo>(groupName, info)); } } for (int j = containerRelated.Count - 1; j >= 0; j--) { MeshRenderer meshR = containerRelated[j] as MeshRenderer; if (meshR != null) { Mesh mesh = Operations.GetMesh(meshR); if (mesh != null) { AssetInfo info = new AssetInfo(file); info.asset = new PPtr <Object>(mesh); containerEntries.Insert(1, new KeyValuePair <string, AssetInfo>(groupName, info)); } } } for (int j = 0; j < containerEntries.Count; j++) { containerEntries[j].Value.preloadSize = assets.Count; } } else { containerEntries[0].Value.preloadSize = assets.Count; } for (int j = 0; j < containerEntries.Count; j++) { if (containerEntries[j].Key != groupName) { containerEntries[j] = new KeyValuePair <string, AssetInfo>(groupName, containerEntries[j].Value); } } return; } } } }
private static void GetDependantAssets(Component asset, List<Component> assets, List<Component> transforms, List<Component> containerRelated) { if (asset != null && !assets.Contains(asset)) { assets.Add(asset); switch (asset.classID1) { case UnityClassID.Animator: Animator animator = (Animator)asset; GetDependantAssets(animator.m_Avatar.asset, assets, transforms, containerRelated); GetDependantAssets(animator.m_Controller.asset, assets, transforms, containerRelated); foreach (Component ren in containerRelated) { GetDependantAssets(ren, assets, null, null); if (ren is MeshRenderer) { MeshRenderer meshR = (MeshRenderer)ren; PPtr<Mesh> meshPtr = Operations.GetMeshPtr(meshR); if (meshPtr != null) { if (meshPtr.m_FileID != 0) { Component found = assets.Find ( delegate(Component c) { if (c is ExternalAsset) { ExternalAsset e = (ExternalAsset)c; return e.FileID == meshPtr.m_FileID && e.PathID == meshPtr.m_PathID; } return false; } ); if (found == null) { ExternalAsset extMesh = new ExternalAsset(); extMesh.FileID = meshPtr.m_FileID; extMesh.PathID = meshPtr.m_PathID; assets.Add(extMesh); } } else if (meshPtr.instance != null && !assets.Contains(meshPtr.instance)) { assets.Add(meshPtr.instance); } } foreach (PPtr<Material> matPtr in meshR.m_Materials) { if (!assets.Contains(matPtr.asset)) { GetDependantAssets(matPtr.asset, assets, null, null); } } } } assets.Sort ( delegate(Component c1, Component c2) { if (c1 is ExternalAsset) { ExternalAsset e1 = (ExternalAsset)c1; if (c2 is ExternalAsset) { ExternalAsset e2 = (ExternalAsset)c2; return e1.FileID != e2.FileID ? e1.FileID.CompareTo(e2.FileID) : e1.PathID.CompareTo(e2.PathID); } return -1; } else if (c2 is ExternalAsset) { return 1; } return c1.pathID.CompareTo(c2.pathID); } ); break; case UnityClassID.Avatar: break; case UnityClassID.AnimatorController: AnimatorController aCon = (AnimatorController)asset; for (int i = 0; i < aCon.m_AnimationClips.Count; i++) { assets.Add(aCon.m_AnimationClips[i].asset); containerRelated.Add(aCon.m_AnimationClips[i].asset); } break; case UnityClassID.AnimationClip: break; case UnityClassID.GameObject: GameObject gameObj = (GameObject)asset; animator = null; foreach (var compPair in gameObj.m_Component) { switch (compPair.Key) { case UnityClassID.Transform: Transform trans = (Transform)compPair.Value.instance; transforms.Add(trans); foreach (Transform child in trans) { GetDependantAssets(child.m_GameObject.asset, assets, transforms, containerRelated); } break; case UnityClassID.Animator: animator = (Animator)compPair.Value.asset; break; case UnityClassID.MeshRenderer: case UnityClassID.MeshFilter: case UnityClassID.SkinnedMeshRenderer: containerRelated.Add(compPair.Value.asset); break; case UnityClassID.MonoBehaviour: GetDependantAssets(compPair.Value.asset, assets, transforms, containerRelated); break; default: if (!assets.Contains(compPair.Value.asset)) { assets.Add(compPair.Value.asset); } break; } } if (animator != null) { foreach (Component trans in transforms) { GetDependantAssets(trans, assets, null, null); } GetDependantAssets(animator, assets, transforms, containerRelated); } break; case UnityClassID.Light: break; default: if (asset.classID2 == UnityClassID.MonoBehaviour) { MonoBehaviour monoB = (MonoBehaviour)asset; if (!assets.Contains(monoB.m_MonoScript.asset)) { assets.Add(monoB.m_MonoScript.asset); } } break; case UnityClassID.MonoScript: break; case UnityClassID.Transform: break; case UnityClassID.MeshRenderer: case UnityClassID.MeshFilter: case UnityClassID.SkinnedMeshRenderer: break; case UnityClassID.Material: Material mat = (Material)asset; foreach (var texVal in mat.m_SavedProperties.m_TexEnvs) { GetDependantAssets(texVal.Value.m_Texture.asset, assets, transforms, containerRelated); } if (mat.m_Shader.m_FileID != 0) { AddExternalAsset(assets, mat.m_Shader); } else if (mat.m_Shader.instance != null) { GetDependantAssets(mat.m_Shader.asset, assets, transforms, containerRelated); } break; case UnityClassID.Shader: Shader shader = (Shader)asset; foreach (PPtr<Shader> dep in shader.m_Dependencies) { if (dep.m_FileID != 0) { AddExternalAsset(assets, dep); } else if (dep.asset != null) { GetDependantAssets(dep.asset, assets, transforms, containerRelated); } } break; case UnityClassID.Sprite: assets.Remove(asset); Sprite sprite = (Sprite)asset; assets.Add(sprite.m_RD.texture.asset); assets.Add(sprite); break; case UnityClassID.Texture2D: break; } } }
private static void AddExternalAsset(List<Component> assets, PPtr<Shader> dep) { Component found = assets.Find ( delegate(Component c) { if (c is ExternalAsset) { ExternalAsset e = (ExternalAsset)c; return e.FileID == dep.m_FileID && e.PathID == dep.m_PathID; } return false; } ); if (found == null) { ExternalAsset extShader = new ExternalAsset(); extShader.FileID = dep.m_FileID; extShader.PathID = dep.m_PathID; assets.Add(extShader); } }