Reputation: 3
I created a toggle group with four toggles and installed swap image to highlighted. In result, I want to have four states for toggle image - background, highlighted, true and false.
That I think, I can do it only with script C#. My attempts to use toggle.targetGrafic
and toggle.grafic
doesn't work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LoadQuestion : MonoBehaviour
{
public Toggle toggleA;
public Toggle toggleB;
public Toggle toggleC;
public Toggle toggleD;
public Image trueA;
public Image falseA;
public Image backA;
public void CheckAnswer()
{
if ( toggleA.isOn )
{
toggleA.targetGraphic = trueA; //Resources.Load("Sprites/logo") as Image;
}
}
}
CheckAnswer()
I was imported to On Value Changed (Boolean) in engine.
Resources.Load
- also does not give the result and reporting error:
You must have a Image target in order to use a sprite swap transition.
Upvotes: 0
Views: 3435
Reputation: 1066
If you want to have custom images when toggling on/off, you can use this: Remove reference to graphic in Toggle component in inspector or by script
toggleA.graphic = null;
Make reference to Image which was that graphic component and change sprite of that image via script;
public Toggle toggleA;
public Image toggleAImage;
public Sprite trueA;
public Sprite falseA;
private bool isOn;
private void Start()
{
toggleA.graphic = null;
}
public void CheckAnswer()
{
toggleAImage.sprite = isOn ? trueA : falseA;
isOn = !isOn;
}
Not sure what you're trying to do with background and highlighted, but you can setup those conditions in inspector on the Toggle component. Please keep in mind, that toggle consists of 2 elements: background image and "check" image. You can setup highlighted state sprite swap in inspector only for background. My code above is for "check" image.
Upvotes: 0