当前位置: 首页>>代码示例>>C#>>正文


C# Texture2D.CopyFrom方法代码示例

本文整理汇总了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();
    }
开发者ID:linojon,项目名称:VirtualOldManC,代码行数:101,代码来源:SimpleLOD_MaterialPopup.cs


注:本文中的UnityEngine.Texture2D.CopyFrom方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。