1
\$\begingroup\$

I have some code that creates objects with an Image component, in which I want to replace the Source Image with one of the default built-in sprites, through the code.

Example:

введите сюда описание изображения

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class PotionManager : MonoBehaviour {

    Image sourceImage;
    private float timer=25;
    static int COUNT_OF_ORDERS = 0;
    public Transform parent;
    public List<PotionAsset> potionList = new List<PotionAsset>();
    private float posX,posY,posZ;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update()
    {
        timer += Time.deltaTime;
        if (timer >= 30)
        {
            CreateOrderHolder();
        }
    }

    void CreateOrderHolder()
    {
        posX = 160 * COUNT_OF_ORDERS + 80;
        posY = -70;
        posZ = 0;

        COUNT_OF_ORDERS++;

        GameObject temp = new GameObject("Potion Order " + COUNT_OF_ORDERS, typeof(RectTransform));
        temp.transform.SetParent(parent);
        temp.transform.localPosition = new Vector3(posX, posY, posZ);
        temp.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f);
        temp.GetComponent<RectTransform>().anchorMax = new Vector2(0, 1);
        temp.GetComponent<RectTransform>().anchorMin = new Vector2(0, 1);

        CreatePotionHolder(temp.transform);

        timer = 0;
    }

    void CreatePotionHolder(Transform parent)
    {
        posX = 0;
        posY = 13.5f;
        posZ = 0;

        GameObject temp = new GameObject("PotionHolder", typeof(RectTransform), typeof(Image));

        temp.transform.SetParent(parent);
        temp.transform.localPosition = new Vector3(posX, posY, posZ);
        temp.transform.localScale = new Vector3(1, 1, 1);
        temp.GetComponent<RectTransform>().sizeDelta = new Vector2(100,50);

        sourceImage = temp.GetComponent<Image>();
        sourceImage.sprite = Resources.Load<Sprite>("Resources/unity_builtin_extra/UISprite");
        Debug.Log(sourceImage.sprite);
    }

}

In the end, sourceImage.sprite returns null. I tried to add at the end of the file path the file extension like png, psd, but this did not help.

\$\endgroup\$
  • \$\begingroup\$ Resources.Load returns null if it can not find the asset under the provided filename. Where exactly is the file located? Also, what kind of file is it? Resources.Load<Sprite> will require an Unity sprite asset at that position. Not an image asset or a .psd file. \$\endgroup\$ – Philipp Jul 24 '19 at 10:43
  • 1
    \$\begingroup\$ Also note that use of a Resources folder is deprecated. The new recommended way of loading assets at runtime is Addressable Assets. \$\endgroup\$ – Philipp Jul 24 '19 at 10:47
  • 3
    \$\begingroup\$ Have you considered attaching this sprite to your script as a public/serialized member variable, so you don't need to go hunting for it at all? Doing this makes the dependency visible to Unity, so it can ensure the sprite is loaded and ready to use when your script is instantiated. \$\endgroup\$ – DMGregory Jul 24 '19 at 11:54
  • \$\begingroup\$ @Philipp I know, that Resources.Load can't find asset under provided filename. That's why I asking my question. Where I can find that sprite(and how to use it properly). It's into my project folder, into my unity folder? Where?) \$\endgroup\$ – Artik Slayer Jul 24 '19 at 12:24
  • \$\begingroup\$ @DMGregory Yeah, i was thinking about your method, but I wanna exactly same sprite. That's why I don't know where I can find it? In my project folder, my unity folder?) \$\endgroup\$ – Artik Slayer Jul 24 '19 at 12:27
2
\$\begingroup\$

You can access the built in Extra Resources in the editor. I believe if you make a public reference to the UI sprite it will create a replica to use for runtime somewhere along the way.

Otherwise the path to the sprite is 'UI/Skin/UISprite.psd'

example

sourceImage = temp.GetComponent<Image>();
sourceImage.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd");

I'd suggest using Sprite.Create(); during runtime and you can create a texture for it also so you have no need to load their default one.

\$\endgroup\$
  • \$\begingroup\$ note that you could also load and make a copy of the default UISprite in the editor and then place the 'copy' as your own resource to load whenever for later use in your project. \$\endgroup\$ – Justin Markwell Jul 24 '19 at 17:57

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.