Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] How to make a Slide Show in Unity?

Discussion in 'Scripting' started by manujarvinen, Aug 18, 2019.

  1. manujarvinen

    manujarvinen

    Joined:
    Dec 11, 2013
    Posts:
    3
    Solved, see the latest post below.



    Whee!


    ---------------------------------------------------------------------------------


    [Total beginner]

    Hi people :)

    I'd like to make a slide show with big buttons, like seen below.



    That's what I have so far.

    The next step obviously is to have the other buttons to deliver 'Slide 2', 'Slide 3', and so on...
    BUT
    There should be only one button pressed down and one slide shown at any given time. How should I go about doing this?

    Here's some of the code snippets (that work really actually on the first button only :/):

    Button1Controller:
    Code (CSharp):
    1.     public Animator button1;
    2.  
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.         button1 = GetComponent<Animator>();
    7.     }
    8.  
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.  
    13.         if (GameObject.Find("button1_cylinder").GetComponent<ButtonMouseClick>().slideState == 1)
    14.         {
    15.             button1.Play("buttonDown");
    16.         }
    17.         if (GameObject.Find("button1_cylinder").GetComponent<ButtonMouseClick>().slideState == 2)
    18.         {
    19.             button1.Play("buttonUp");
    20.         }

    ButtonMouseClick:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.SceneManagement;
    5. public class ButtonMouseClick : MonoBehaviour
    6. {
    7.  
    8.     public GameObject slide1;
    9.     public int slideState;
    10.  
    11.     public Color startColor;
    12.     public Color mouseOverColor;
    13.     bool isTriggered = false;
    14.  
    15.     void Start()
    16.     {
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         if (slideState == 1)
    22.         {
    23.             if (isTriggered && Input.GetMouseButtonDown(0))
    24.             {
    25.                 slideState = 2;
    26.             }
    27.         }
    28.         else
    29.         {
    30.             if (isTriggered && Input.GetMouseButtonDown(0))
    31.             {
    32.                 slideState = 1;
    33.             }
    34.         }
    35.     }
    36.  
    37.  
    38.     // Use this for initialization
    39.     void OnTriggerEnter(Collider other)
    40.     {
    41.         if (other.CompareTag("Player"))
    42.         {
    43.             Debug.Log("CONTACT Enter Player");
    44.             isTriggered = true;
    45.             GetComponent<Renderer>().material.SetColor("_Color", mouseOverColor);
    46.         }
    47.     }
    48.  
    49.     void OnTriggerExit(Collider other)
    50.     {
    51.         if (other.CompareTag("Player"))
    52.         {
    53.             Debug.Log("CONTACT Exit");
    54.             isTriggered = false;
    55.             GetComponent<Renderer>().material.SetColor("_Color", startColor);
    56.         }
    57.     }
    58.  
    59.  
    60. }
    61.  

    Slide1Controller:
    Code (CSharp):
    1. [/SIZE]
    2.     public Animator slide1;
    3.  
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.         slide1 = GetComponent<Animator>();
    8.     }
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.  
    14.         if (GameObject.Find("button1_cylinder").GetComponent<ButtonMouseClick>().slideState == 1)
    15.         {
    16.             slide1.Play("fromBottomToTop");
    17.         }
    18.         if (GameObject.Find("button1_cylinder").GetComponent<ButtonMouseClick>().slideState == 2)
    19.         {
    20.             slide1.Play("FromTopToBottom");
    21.         }
    22.     }
     
    Last edited: Sep 6, 2019
  2. manujarvinen

    manujarvinen

    Joined:
    Dec 11, 2013
    Posts:
    3
    Yeehaa, got my friend to get me started, here's what he provided: example Unity Slideshow (.zip) (or the attachment)



    SlideManager.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SlideManager : MonoBehaviour
    6. {
    7.     public GameObject[] m_slides;
    8.  
    9.     private void Start()
    10.     {
    11.         // Lets hide all slides on start
    12.         for (int i = 0; i < m_slides.Length; i++)
    13.         {
    14.             m_slides[i].SetActive(false);
    15.         }
    16.     }
    17.  
    18.     public void ShowSlide(GameObject obj)
    19.     {
    20.         // Hide all slides
    21.         for(int i = 0; i < m_slides.Length; i++)
    22.         {
    23.             m_slides[i].SetActive(false);
    24.         }
    25.  
    26.         // Show selected slide
    27.         obj.SetActive(true);
    28.     }
    29. }
    Button_ShowSlide.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Button_ShowSlide : MonoBehaviour
    6. {
    7.     public GameObject m_slide;
    8.     private SlideManager m_slideManager;
    9.  
    10.     public Animation m_animator;
    11.     public AnimationClip m_clipDown;
    12.     public AnimationClip m_clipUp;
    13.  
    14.     private void Start()
    15.     {
    16.         m_slideManager = FindObjectOfType<SlideManager>();
    17.     }
    18.  
    19.     // Call slidemanager's ShowSlide function on MouseDown
    20.     public void OnMouseDown()
    21.     {
    22.         m_animator.Play(m_clipDown.name);
    23.         m_slideManager.ShowSlide(m_slide);
    24.     }
    25.  
    26.     public void OnMouseUp()
    27.     {
    28.         m_animator.Play(m_clipUp.name);
    29.         m_slideManager.ShowSlide(m_slide);
    30.     }
    31. }
     

    Attached Files:

  3. manujarvinen

    manujarvinen

    Joined:
    Dec 11, 2013
    Posts:
    3
  4. Tawny-la-Gargouille

    Tawny-la-Gargouille

    Joined:
    Nov 5, 2020
    Posts:
    2