slimecing

a fighting game featuring slimes and swords
Log | Files | Refs | README

ListSelector.cs (1600B)


      1 using System.Collections;
      2 using System.Collections.Generic;
      3 using UnityEngine;
      4 using UnityEngine.UI;
      5 
      6 public class ListSelector : MonoBehaviour
      7 {
      8     public GameObject shownImage;
      9     public GameObject indexObject;
     10     public int player;
     11     public int controller = -1;
     12 
     13     private HatIndex index;
     14     private int current;
     15     private Image img;
     16     private float nextTime = 0;
     17     // Start is called before the first frame update
     18     void Start()
     19     {
     20         index = indexObject.GetComponent<HatIndex>();
     21         img = shownImage.GetComponent<Image>();
     22         index.Set(player, index.objects[0]);
     23     }
     24 
     25     private void Update()
     26     {
     27         if(controller!=-1)
     28         {
     29             if (Input.GetAxis("jHorizontal"+(controller+1))>=0.5&&Time.time>nextTime)
     30             {
     31                 NextSelection();
     32                 nextTime = Time.time + 0.2f;
     33             }
     34             else if (Input.GetAxis("jHorizontal" + (controller+1)) <= -0.5 && Time.time > nextTime)
     35             {
     36                 LastSelection();
     37                 nextTime = Time.time + 0.2f;
     38             }
     39         }
     40     }
     41 
     42     public void LastSelection()
     43     {
     44         current--;
     45         if (current < 0)
     46         {
     47             current = index.images.Length - 1;
     48         }
     49         img.sprite = index.images[current];
     50         index.Set(player, index.objects[current]);
     51     }
     52 
     53     public void NextSelection()
     54     {
     55         current++;
     56         if (current > index.images.Length - 1)
     57         {
     58             current = 0;
     59         }
     60         img.sprite = index.images[current];
     61         index.Set(player, index.objects[current]); 
     62     }
     63 }