title: Drawing to a texture in Unity is very slow
body: <p>I have been learning Unity for the last few weeks in order to create a simple ant simulation. The way I was rendering everything was writing to a texture the size of the camera in the <code>Update</code> function. It works, but problem is that it is extremely slow, getting only around 3-4 FPS doing so. What could be done to speed it up? Maybe a completely different way of rendering?</p>
  <p>Here is the code of a simple test where some Ants just move around in random directions. I have the <code>AntScript.cs</code> attached to the camera with a texture under a <code>Canvas</code> where everything is being written to.</p>
  <p><code>AntScript.cs</code></p>
  <pre class="lang-cs prettyprint-override"><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  using System;
  
  public class Ant 
  {
      public int thisX, thisY, val;
      public bool carryingFood = false;
      
      public Ant(int x, int y) 
      {
          thisX = x;
          thisY = y;
      }
  }
  
  public class AntScript : MonoBehaviour 
  {
      private Texture2D myTexture;
      public int imageWidth = 1920;
      public int imageHeight = 1080;
      public int gridWidth = 160;
      public int gridHeight = 90;
      private int cellSize;
      public GameObject imageElement;
  
      private List&lt;Ant&gt; ants = new List&lt;Ant&gt;();
  
      void Start() 
      {
          // Initialise texture
  
          myTexture = new Texture2D(
              imageWidth, 
              imageHeight, 
              TextureFormat.ARGB32, 
              false
          );
          
          // Calculate the size of a grid pixel
          cellSize = (int)Math.Min(
              imageWidth / gridWidth, 
              imageHeight / gridHeight
          );
          Debug.Log(cellSize);
   
          // Add test ants
          for (int i = 0; i &lt; 5; i++) 
          {
              for (int j = 0; j &lt; 5; j++) 
              {
                  ants.Add(new Ant(i * 4, j * 4));
              }
          }
      }
  
      void updImage() 
      {
          System.Random rnd = new System.Random();
          List&lt;int&gt; xDirs = new List&lt;int&gt; {0, 1, 1, 1, 0, -1, -1, -1};
          List&lt;int&gt; yDirs = new List&lt;int&gt; {1, 1, 0, -1, -1, -1, 0, 1};
  
  /*--------------------- UPDATE ALL ELEMENTS ---------------------*/
  
          // Move ants
          foreach (Ant ant in ants) 
          {
              int randDirInt = rnd.Next(0, 8);
              ant.thisX += xDirs[randDirInt];
              ant.thisY += yDirs[randDirInt];
          }
  
  /*--------------------- DRAW ALL ELEMENTS ---------------------*/
  
          // Clear texture
          for (int i = 0; i &lt; imageWidth; i++) 
          {
              for (int j = 0; j &lt; imageHeight; j++) 
              {
                  myTexture.SetPixel(i, j, Color.black);
              }
          }
          
          // Draw ants
          foreach (Ant ant in ants) 
          {
              for (int i = 0; i &lt; cellSize; i++) 
              {
                  for (int j = 0; j &lt; cellSize; j++) 
                  {
                      myTexture.SetPixel(
                          ant.thisX * cellSize + i, 
                          ant.thisY * cellSize + j, 
                          Color.white
                      );
                  }
              }
          }
  
          myTexture.Apply();
  
          imageElement.GetComponent&lt;Image&gt;().sprite = Sprite.Create(
              myTexture, 
              new Rect(0, 0, imageWidth, imageHeight), 
              new Vector2(0.5f, 0.5f)
          );
      }
      
      void Update() 
      {
          updImage();
      }
  }
  </code></pre>
  <p>Elements in scene:</p>
  <p><a href="https://i.stack.imgur.com/lMDG7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lMDG7.png" alt="elements" /></a></p>
  <p>Script arguments:</p>
  <p><a href="https://i.stack.imgur.com/sTSFf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sTSFf.png" alt="script args" /></a></p>