Unity3d 在运行时合并纹理

Unity3d 在运行时合并纹理,unity3d,textures,Unity3d,Textures,除了使用SetPixels()外,是否有其他方法可以将一个纹理“烘焙”到另一个纹理? 现在我尝试使用类似的方法,但速度太慢了: public static Texture2D CombineTextures(Texture2D aBaseTexture, Texture2D aToCopyTexture, int x, int y) { int aWidth = aBaseTexture.width; int aHeight = aBaseTexture.height;

除了使用SetPixels()外,是否有其他方法可以将一个纹理“烘焙”到另一个纹理? 现在我尝试使用类似的方法,但速度太慢了:

public static Texture2D CombineTextures(Texture2D aBaseTexture, Texture2D aToCopyTexture, int x, int y)
{
    int aWidth = aBaseTexture.width;
    int aHeight = aBaseTexture.height;

    int bWidth = aToCopyTexture.width;
    int bHeight = aToCopyTexture.height;

    Texture2D aReturnTexture = new Texture2D(aWidth, aHeight, TextureFormat.RGBA32, false);

    Color[] aBaseTexturePixels = aBaseTexture.GetPixels();
    Color[] aCopyTexturePixels = aToCopyTexture.GetPixels();

    int aPixelLength = aBaseTexturePixels.Length;
    for(int y1 = y, y2 = 0; y1 < aHeight && y2 < bHeight ; y1++, y2++)
    {
        for(int x1 = x, x2 = 0 ; x1 < aWidth && x2 < bWidth; x1++, x2++)
        {
            aBaseTexturePixels[x1 + y1*aWidth] = Color.Lerp(aBaseTexturePixels[x1 + y1*aWidth], aCopyTexturePixels[x2 + y2*bWidth], aCopyTexturePixels[x2 + y2*bWidth].a);
        }
    }

    aReturnTexture.SetPixels(aBaseTexturePixels);
    aReturnTexture.Apply(false);

    return aReturnTexture;
}
public static Texture2D CombineTextures(Texture2D aBaseTexture,Texture2D atopycytexture,int x,int y)
{
int aWidth=aBaseTexture.width;
int aHeight=aBaseTexture.height;
int bWidth=aToCopyTexture.width;
int bHeight=aToCopyTexture.height;
Texture2D aReturnTexture=新的Texture2D(宽度,高度,TextureFormat.RGBA32,假);
Color[]aBaseTexturePixels=aBaseTexture.GetPixels();
Color[]aCopyTexturePixels=aToCopyTexture.GetPixels();
int apixelength=aBaseTexturePixels.Length;
对于(inty1=y,y2=0;y1

问题是,我需要在2d表面上显示很多精灵(血液、敌人尸体等),而仅仅实例化每个精灵将大大减少fps。

如果你在实例化预制件时担心fps下降,你肯定应该建立一个对象池系统。因此,您将拥有一个系统:

  • 实例化池中的所有对象并使其远离主摄影机
  • 一旦您需要该对象,您将从池中“借用”它
  • 一旦不再需要对象,您将将其返回到对象池(例如,当sprite不在摄影机视图中时)
  • 把它全部烘焙到一个纹理不是最好的做法。你需要大量的RAM。考虑上面的步骤,它非常常见的实践

    这里有一个很好的例子:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System;
    using System.Linq;
    
    public class BackgroundPool : MonoBehaviour 
    {
        public static BackgroundPool instance;
        public List<BackgroundSection> sectionsLibrary = new List<BackgroundSection>();
        public int poolSize = 4;
        public List<BackgroundSection> pool = new List<BackgroundSection>();
    
    
        void Awake()
        {
            instance = this;
    
            DateTime startGenTime = DateTime.Now;
    
            //generateSectionsPool
            for (int i=0; i<sectionsLibrary.Count; i++)
            {
                for (int j=0; j<poolSize; j++)
                {
                    if (j == 0) 
                    {
                        sectionsLibrary[i].positionInPool = sectionsLibrary[i].transform.position;
                        pool.Add(sectionsLibrary[i]);
                    }
                    else
                    {
                        BackgroundSection section = (BackgroundSection)Instantiate(sectionsLibrary[i]);
    
                        section.transform.parent = this.transform;
                        section.transform.position = new Vector3((-(ExtensionMethods.GetBounds(sectionsLibrary[i].gameObject).extents.x * 2) * j) + sectionsLibrary[i].transform.position.x,
                                                             sectionsLibrary[i].transform.position.y);
                        section.transform.localEulerAngles = Vector3.zero;
    
                        section.gameObject.name = sectionsLibrary[i].gameObject.name + ":" + j.ToString();
                        section.positionInPool = section.transform.position;
                        pool.Add(section);  
                    }
               }
            }
            Debug.Log("Background Pool generated in: " + (DateTime.Now - startGenTime).TotalSeconds.ToString() + " s");
        }
    
    
        public BackgroundSection GetPiece(Scenery scenery, SceneryLayer _layer)
        {
            List<BackgroundSection> allScenery = new List<BackgroundSection>();
            foreach (BackgroundSection section in pool) { if (section.scenery == scenery) allScenery.Add(section); }
    
            List<BackgroundSection> matchingPieces = new List<BackgroundSection>();
            foreach (BackgroundSection section in allScenery)  { if (section.sceneryLayer == _layer) matchingPieces.Add(section); }
    
            if (matchingPieces.Count > 0) 
            {
                BackgroundSection pickedSection = matchingPieces[UnityEngine.Random.Range(0,matchingPieces.Count-1)];
                pool.Remove(pickedSection);
                return pickedSection;
            }
            else
            {
                Debug.LogError("Cann't get background piece matching criteria, scenery: " + scenery + ", layer" + _layer);
                return null;
            }
        }
    
    
        public void ReturnPiece(BackgroundSection section)
        {
            pool.Add(section);
            section.transform.parent = this.transform;
            section.transform.position = section.positionInPool;
        }
    }
    
    使用UnityEngine;
    使用系统集合;
    使用System.Collections.Generic;
    使用制度;
    使用System.Linq;
    公共课堂背景:单一行为
    {
    公共静态背景池实例;
    公共列表部分库=新列表();
    公共int池大小=4;
    公共列表池=新列表();
    无效唤醒()
    {
    实例=此;
    DateTime startGenTime=DateTime.Now;
    //generateSectionsPool
    
    对于(int i=0;我将进入堆栈溢出!请编辑您的问题,并添加有关您迄今为止尝试的内容的信息。