- Home /
Changing images using a shader on a 2D canvas?
I'm trying to create a character select screen that uses a single square for each player and takes controller input to scroll through an array of images to select the character they want. I have most of it down. The trouble is that while the shader on the square reflects the image I want to see in the inspector, the image in the actual game doesn't change unless I toggle something in the shader inspector!
Here is my code, which includes the array for the actual fighter select.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class FighterSelection : MonoBehaviour {
public int index = 0;
public bool caninteract;
public string Horiz;
public string Vert;
public string fighterfolder;
public string P;
public GameObject Im;
public List<GameObject> fighterlist;
public List<Texture> plist;
public string TextureName;
void Start (){
caninteract = true;
Texture[] pics = Resources.LoadAll<Texture> ("Images/Portraits");
GameObject[] fighter = Resources.LoadAll<GameObject>(fighterfolder);
foreach (GameObject f in fighter) {
GameObject fightr = Instantiate(f) as GameObject;
fightr.transform.SetParent(GameObject.Find(P).transform);
fighterlist.Add (fightr);
fightr.SetActive (false);
fighterlist [index].SetActive (true);
foreach (Texture i in pics) {
Texture pc = Instantiate (i) as Texture;
plist.Add (pc);
}
}
}
void Update(){
if (caninteract == true);
{
if (Input.GetAxis (Horiz) > 0f) {
ToggleRight ();
StartCoroutine (select ());
}
if (Input.GetAxis (Horiz) < 0f) {
ToggleLeft ();
StartCoroutine (select ());
}
}
Texture portrait = (Texture)plist [index];
Im.GetComponent<Image> ().material.mainTexture = portrait;
Im.GetComponent<Image> ().material.SetColor ("_Color", Color.clear);
}
IEnumerator select ()
{
yield return new WaitForSeconds (0.75f);
caninteract = true;
}
public void ToggleLeft()
{
fighterlist [index].SetActive (false);
index --;
caninteract = false;
if (index < 0) {
index = fighterlist.Count - 1;
}
fighterlist [index].SetActive (true);
}
public void ToggleRight ()
{
fighterlist[index].SetActive (false);
index++;
caninteract = false;
if (index == fighterlist.Count) {
index = 0;
}
fighterlist [index].SetActive (true);
}
}
The variables I put in the inspector are to separate the script by player. I also have a problem getting the waitforseconds thing to work, but that can wait. What am I doing wrong?
Your answer
Follow this Question
Related Questions
Sprite Shader not working in 5.6 0 Answers
Blending 2d light sprites 1 Answer
How to do 2D graphics like this ? 1 Answer
I Installed URP But I Can't Create 2D Shaders 1 Answer
Replicate CSS3 animation in Unity 0 Answers