0
\$\begingroup\$

I have been trying to write a method that fills a rectangle with a color. I've tried it as a new 2D and 3D project just in case, but this gives the same results.

Basically, i want the script to make a new GameObject, give it a SpriteRenderer component and then fill the given rectangle with the given color.

I have tried to work from the Unity docs and other sources of info, but no matter how much I look around, this now looks correct to me but doesnt work.

The result (as pictured) shows the new object with the sprite renderer and even shows the right size (there are blue 'corner' icons in the scene view I can see but no sprite or rectangle of any kind.

using UnityEngine;


public class GameBoard : MonoBehaviour
{
    const ushort cellCount_x = 10;
    const ushort cellCount_y = 24; // only 20 will be visible to player
    const ushort cellsize = 50;

    GameObject testBlock;
    SpriteRenderer testBlockRen;

    private void Start()
    {
        testBlock = new GameObject();
        testBlockRen = testBlock.AddComponent<SpriteRenderer>();
        testBlockRen.sprite = CreateRectSprite(new Rect(0, 0, cellsize, cellsize), Color.red);
    }

    Sprite CreateRectSprite(Rect rect, Color color)
    {
        Texture2D tex = Texture2D.whiteTexture;
        tex.SetPixel(0, 0, color);
        tex.Resize((int)rect.width, (int)rect.height);
        Sprite sprite = Sprite.Create(tex, rect, Vector2.zero);
        return sprite;
    }
}

enter image description here

By the way: I have made sure the camera 'z' is less than 0 (it's -10). And the camera is centred where the sprite is supposed to appear.

Note: I can get a sprite to render fine if I create a texture in gimp and then use Resources.Load() . But I am trying to create the sprite using no source texture whatsoever (other than Texture2d.whitetexture)

\$\endgroup\$
1
\$\begingroup\$

You can make sprite with code by using Sprite.Create

https://docs.unity3d.com/ScriptReference/Sprite.Create.html

Sprite.Create creates a new Sprite which can be used in game applications. A texture needs to be loaded and assigned to Create in order to control how the new Sprite will look. In the script example below a new Sprite is displayed when the button is pressed. The new sprite is created in Start.

red rectangle

enter image description here

using UnityEngine;

public class SimpleRectangle: MonoBehaviour
{
    private Sprite mySprite;
    private SpriteRenderer sr;
    public Color color = Color.red;

    void Awake()
    {
        sr = GetComponent<SpriteRenderer>();
        sr.color = color;

        transform.position = new Vector3(1.5f, 1.5f, 0.0f);
    }

    void Start()
    {
        Texture2D tex = new Texture2D(500,250);
        mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
        sr.sprite = mySprite;
    }
}

another example , simple checker texture with code: try to change height and width in editor

enter image description here

using UnityEngine;

[ExecuteInEditMode]
public class SimpleChecker: MonoBehaviour
{
    [Range(1,10)]
    public int height = 10,width = 10;

    private void Update() {
    Texture2D texture = new Texture2D(128, 128);
    GetComponent<SpriteRenderer>().sprite = Sprite.Create(CreateTexture(texture), new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100.0f);

    }
    public Texture2D CreateTexture(Texture2D texture){

        for (int y = 0; y < texture.height; y++)
        {
            for (int x = 0; x < texture.width; x++)
            {
                Color color = (x/height % 2 + y/width % 2 ) == 1 ? Color.white : Color.black;
                texture.SetPixel(x, y, color);
            }
        }
        texture.Apply();
    return texture;
    }
}

Update

your code didn't work because you forgot to use tex.Apply();

using UnityEngine;


public class GameBoard : MonoBehaviour
{
  const ushort cellCount_x = 10;
    const ushort cellCount_y = 24; // only 20 will be visible to player
    const ushort cellsize = 50;

    GameObject testBlock;
    SpriteRenderer testBlockRen;

    private void Start()
    {
        testBlock = new GameObject();
        testBlockRen = testBlock.AddComponent<SpriteRenderer>();
        testBlockRen.sprite = CreateRectSprite(new Rect(0, 0, cellsize, cellsize),Color.red);
    }

    Sprite CreateRectSprite(Rect rect,Color color)
    {
        Texture2D tex = Texture2D.whiteTexture;
        tex.Resize((int)rect.width, (int)rect.height);
        for (int x = 0; x < (int)rect.width; x++){
             for (int y = 0; y < rect.height; y++){
                 tex.SetPixel(x, y, color); 
             }
         }
        tex.Apply();
        Sprite sprite = Sprite.Create(tex, rect, Vector2.zero);
        return sprite;
    }
}
\$\endgroup\$
  • \$\begingroup\$ Hi mate thanks for the help here. However are you able to see why my call to Sprite.Create (near the bottom of my original post code) doesnt make the texture/sprite appear on screen? thanks \$\endgroup\$ – Big T Larrity Jun 14 at 23:22
  • 1
    \$\begingroup\$ @BigTLarrity because you forgot to add tex.Apply(); \$\endgroup\$ – Seyed Morteza Kamali Jun 15 at 4:00
  • \$\begingroup\$ Thank you good sir!! \$\endgroup\$ – Big T Larrity Jun 16 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.