本文整理汇总了C#中UnityEngine.Texture2D.CopyFrom方法的典型用法代码示例。如果您正苦于以下问题:C# Texture2D.CopyFrom方法的具体用法?C# Texture2D.CopyFrom怎么用?C# Texture2D.CopyFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Texture2D
的用法示例。
在下文中一共展示了Texture2D.CopyFrom方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateAtlas
//.........这里部分代码省略.........
for(int j=0;j<atlasRows.Count;j++) {
Material m = (Material)atlasRows[j]["origMat"];
Texture tex = null;
if(m.HasProperty(key)) tex = m.GetTexture(key);
if(tex != null && tex.GetType() != typeof(Texture2D)) tex = null;
texturesToInclude.Add((Texture2D)tex);
}
}
if(texturesToInclude.Count <= keys.Count) { // Only 1 row included in atlas
for(int j=0;j<atlasRows.Count;j++) {
origMat = (Material)atlasRows[j]["origMat"];
atlasMat = (Material)atlasRows[j]["atlasMat"];
for(int i=0;i<keys.Count;i++) {
string key = keys[i];
Texture tex = null;
if(origMat.HasProperty(key)) tex = origMat.GetTexture(key);
if(atlasMat.HasProperty(key)) atlasMat.SetTexture(key, tex);
}
}
} else { // make an atlas per key
string key = keys[0];
List<Texture2D> tList = new List<Texture2D>();
List<int> rowIndexes = new List<int>();
int nrOfUniqueTextures = 0;
for(int j=0;j<atlasRows.Count;j++) {
Texture2D t = texturesToInclude[j];
if(t != null) {
float atlasSize = (float)((int)atlasRows[j]["atlasSize"]) / 2f;
float scale = 1f / atlasSize;
if(scale != 1f) t = t.ScaledCopy((int)(t.width * scale), (int)(t.height * scale), false);
int k=0;
for(;k<tList.Count;k++) {
if(tList[k] == t) break;
}
if(k>=tList.Count) nrOfUniqueTextures++;
tList.Add(t);
rowIndexes.Add(j);
}
}
Rect[] rects = null;
Texture2D atlas = null;
if(nrOfUniqueTextures == 1) {
for(int j=0;j<atlasRows.Count;j++) {
origMat = (Material)atlasRows[j]["origMat"];
atlasMat = (Material)atlasRows[j]["atlasMat"];
for(int i=0;i<keys.Count;i++) {
key = keys[i];
Texture tex = null;
if(origMat.HasProperty(key)) tex = origMat.GetTexture(key);
if(atlasMat.HasProperty(key)) atlasMat.SetTexture(key, tex);
}
atlasRows[j]["isAtlas"] = false;
}
} else if(tList.Count>1) {
atlas = new Texture2D(512, 512);
rects = atlas.PackTextures(tList.ToArray(), 0, maxAtlasSize);
masterRow["atlasMaxSize"] = Mathf.Max(atlas.width, atlas.height);
for(int j=0;j<rowIndexes.Count;j++) { // clear current texture
Hashtable row = atlasRows[rowIndexes[j]];
atlasMat = (Material)row["atlasMat"];
row["atlasRect"] = new Rect(0,0,1,1);
if(atlasMat.HasProperty(key)) atlasMat.SetTexture(key, null);
}
for(int j=0;j<rowIndexes.Count;j++) { // set atlas texture
Hashtable row = atlasRows[rowIndexes[j]];
atlasMat = (Material)row["atlasMat"];
row["atlasRect"] = rects[j];
if(atlasMat.HasProperty(key)) atlasMat.SetTexture(key, atlas);
row["isAtlas"] = true;
}
for(int i=1;i<keys.Count;i++) {
key = keys[i];
Texture2D subAtlas = new Texture2D(atlas.width, atlas.height, TextureFormat.RGBA32, true);
subAtlas.Fill(new Color(0,0,0,0));
for(int j=0;j<rowIndexes.Count;j++) { // clear current texture
Hashtable row = atlasRows[rowIndexes[j]];
Rect atlasRect = (Rect)row["atlasRect"];
origMat = (Material)row["origMat"];
if(origMat.HasProperty(key) && atlasMat.HasProperty(key)) {
Texture t = origMat.GetTexture(key);
if(t != null && t.GetType() == typeof(Texture2D)) {
if(key == "_BumpMap" || key == "_NormalMap" || key == "_DetailBumpMap" || key == "_DetailNormalMap") {
t = ((Texture2D)t).FromUnityNormalMap();
}
t = ((Texture2D)t).ScaledCopy(Mathf.RoundToInt(atlas.width * atlasRect.width), Mathf.RoundToInt(atlas.height * atlasRect.height), false);
subAtlas.CopyFrom((Texture2D)t, Mathf.RoundToInt(atlas.width * atlasRect.x), Mathf.RoundToInt(atlas.height * atlasRect.y), 0, 0, t.width, t.height);
atlasMat.SetTexture(key, subAtlas);
}
}
}
}
}
}
Resources.UnloadUnusedAssets();
}