0
\$\begingroup\$

I'm trying to create a level editor for a 2D game and I wanted to instantiate radio buttons, the backgrounds and numbers of which are determined by the tilesheet I load.

I'm sure I'm doing something simple wrong but I can't figure it out. When I instantiate these radio buttons and set their locations and backgrounds to the values I want, they appear in the object hierarchy and each have the correct location but are not displayed. The "Toggle" resource I'm using is the basic Unity toggle object saved in my resources folder. In the screenshot you can see in the scene view that they appear to be in the correct location (the large box at the bottom) since the bounds for the rect transforms show up in the right spot.

instantiated in hierarchy

In the script I'm using, my tileset is sliced into 32x32 squares using unity's slicing tool and has been placed into the script's public tilesheets[] array. It is then used to load all the component sprites, each of which should be used to make a unique radio button. These radio buttons are all parented to the transform of the script, which is the toggleGroup "group_sprites". The script is as follows:

using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.Collections;
using System.Linq;

public class SpriteSelector : MonoBehaviour {

    ToggleGroup group_sprites;
    Toggle[][] toggles;
    public Texture2D[] tilesheets;
    public Texture2D checkmark;
    int sliceSize = 32;
    int level; 

    public void Setup()
    {
        group_sprites = GameObject.Find ("group_sprites").GetComponent<ToggleGroup> ();
        //Slice the current thingy
        LoadTileset (0);

        //Make a toggle group
    }

    public void LoadTileset(int _level)
    {
        level = _level;
        //Slice em up
        string spriteSheet = AssetDatabase.GetAssetPath(tilesheets[level]);
        Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath (spriteSheet)
            .OfType<Sprite> ().ToArray ();

        RectTransform parentRect = gameObject.GetComponent<RectTransform> ();
        int buffer = 1;
        float toggleWidth = parentRect.rect.width/8 - buffer;
        float xPos = -76.45f;
        float yPos = 42.3f;
        foreach(Sprite sprite in sprites)
        {
            GameObject terrainToggle = (GameObject)GameObject.Instantiate(Resources.Load ("Toggle"));
            terrainToggle.transform.SetParent(transform);
            GameObject Background = terrainToggle.transform.FindChild("Background").gameObject;
            GameObject CheckMark = Background.transform.FindChild("Checkmark").gameObject;

            //location
            RectTransform trans = terrainToggle.GetComponent<RectTransform>();
            trans.sizeDelta = new Vector2(toggleWidth, toggleWidth);
            trans.anchoredPosition = new Vector2(xPos ,yPos);//+ trans.sizeDelta.x/2, yPos - trans.sizeDelta.y/2);

            //background
            //Background.GetComponent<Image>().overrideSprite = sprite;
            Image backImage  = Background.GetComponent<Image>();
            backImage.sprite = sprite;
            //checkmark
            CheckMark.GetComponent<Image>().sprite = Sprite.Create (checkmark, new Rect(0,0,checkmark.width, checkmark.height), new Vector2(0.5f,0.5f));


            xPos += parentRect.rect.width/8;
            if(xPos > parentRect.rect.width/2)
            {
                xPos = -76.45f;
                yPos -= parentRect.rect.width/8;
            }
        }

        //foreach slice, make a toggle
    }

    // Update is called once per frame
    void Update () {

        //if click on a tile,
    }
}

Thanks!!

\$\endgroup\$
  • \$\begingroup\$ If you just drag and drop the prefab at runtime, can you see it or does it have the same problem? If it does, then it's not your scripting.. \$\endgroup\$ – Mikael Högström Aug 20 '15 at 12:37
  • \$\begingroup\$ I'm able to see those prefabs, so I suppose I'm instantiating or modifying incorrectly. \$\endgroup\$ – Stephen Schroeder Aug 20 '15 at 21:01
1
\$\begingroup\$

For anyone interested, my problem was that the prefabs were instantiated as part of an Awake() call, which evidently makes your prefabs invisible. Moving the instantiation to a Start() call fixed my problem.

\$\endgroup\$

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.