Im call Texture2D функция on void OnGUI(). И Im добавляет 5 сборных за 5 секунд, и они уничтожаются за 20 секунд. Это все готово, используя Texture2D. Я не разрушаю текстуру после использования. Когда я запускаю эту игру, она использует много памяти, и она растет. Шоуд Мне нужно уничтожить эту сгенерированную текстуру?Проблемы с управлением памятью в Unity3d
public static Texture2D Colors(int r,int g, int b)
{
Texture2D texture = new Texture2D(2, 2);
for (int y = 0; y < texture.height; ++y)
{
for (int x = 0; x < texture.width; ++x)
{
Color color = new Color(r, g, b);
texture.SetPixel(x, y, color);
}
}
texture.Apply();
return texture;
}
Редактировать
Ok. Это то, что я действительно пытаюсь сделать. У меня есть класс с именем HealthSystem On Github
using UnityEngine;
using System.Collections;
public class HealthSystem : MonoBehaviour {
float healthBarLenght=20f;//Length for Healthbar;
public static void Set_HealthBar(Transform transform,int HealthBarHeight, float CurrentHealth,float MaxHealth,Texture2D BackBar, Texture2D FrontBar)
{
Vector3 screenPosition;
GUIStyle style1 = new GUIStyle();
GUIStyle style2 = new GUIStyle();
float HPDrop = (CurrentHealth/MaxHealth) * healthBarLenght;
screenPosition = Camera.main.WorldToScreenPoint(transform.position);
screenPosition.y = Screen.height - screenPosition.y;
style1.normal.background = BackBar;
GUI.Box(new Rect(screenPosition.x-(healthBarLenght/2),screenPosition.y-20, healthBarLenght,HealthBarHeight),"",style1);
style2.normal.background = FrontBar;
GUI.Box(new Rect(screenPosition.x-(healthBarLenght/2),screenPosition.y-20, HPDrop,HealthBarHeight),"",style2);
}
public static Texture2D Colors(int r,int g, int b)
{
Texture2D texture = new Texture2D(2, 2);
for (int y = 0; y < texture.height; ++y)
{
for (int x = 0; x < texture.width; ++x)
{
Color color = new Color(r, g, b);
texture.SetPixel(x, y, color);
}
}
texture.Apply();
return texture;
}
}
Im Calling это из другого сценария
void Start() {
colorback = HealthSystem.Colors (0, 0, 0);
colorfront = HealthSystem.Colors (0, 255, 0);
}
void OnGUI(){
HealthSystem.Set_HealthBar (transform, 2f, 70f, 100f, colorback, colorfront);
}
Этот скрипт привязан к своим eminy войск. 5 противников отбрасывают на 5 секунд. И они будут уничтожены через 20 секунд. Этот объект врага использует физику. ОверлапSphere для идентификации своих врагов
«Должен ли я удалить?» Да, попробуй это. – TuukkaX
Im новое для единства. Итак, как уничтожить текстуру в этой функции. –
Хм, позвольте мне просто подумать об этом. Почему вы используете для циклов, если знаете, что текстура Texture2D всегда 2x 2y? Не могли бы вы также показать код onGUI()? :) – TuukkaX