LoginSignup
7
7

More than 5 years have passed since last update.

uGUIでルーレット

Last updated at Posted at 2015-09-03

昨日はグラフを作って終わってしまったのでルーレットつくりました。
普通のルーレットと、指定したところしか止まらないいかさまルーレットを・・・。
そして気づくuGUIの勉強にはあまりなっていないということに・・・。

普通のルーレット

いかさまルーレット

どこでとめても-30度のピンクしかあたらないいかさまルーレット

リファクタリングなどしていないので作成したままのやっつけソースですが・・・。

普通のルーレットソース


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

public class RouletteManager3 : MonoBehaviour {

    public Transform roulette;
    public GameObject stopBtn;
    public GameObject startBtn;
    public Text resultText;

    private int[] sizeList;
    private float nowSpeed = 0;
    private float MAX_SPEED = 360f;
    private bool isStart = false;
    private bool isStop = false;
    private readonly string[] RESULT_COLOR = new string[]{"ピンク","黄色","紫","緑","水色","赤"};

    void Awake()
    {
        stopBtn.SetActive(false);
        resultText.text = "";
    }

    public void RotateStart()
    {
        resultText.text = "";
        isStart = true;
        stopBtn.SetActive(false);
        startBtn.SetActive(false);
    }

    void Update()
    {
        if(isStart)
        {
            if(!isStop)
            {
                if(nowSpeed < MAX_SPEED)
                {
                    nowSpeed += 5f;
                }
                else
                {
                    stopBtn.SetActive(true);
                }
            }
            else
            {
                nowSpeed -= 5f;
            }

            roulette.localEulerAngles = new Vector3(0, 0, roulette.localEulerAngles.z + nowSpeed * -Time.deltaTime);

            if(nowSpeed <= 0)
            {
                isStart = false;
                isStop = false;
                startBtn.SetActive(true);
                string color = RESULT_COLOR[Mathf.FloorToInt((roulette.localEulerAngles.z / 60))];
                Debug.LogWarning(roulette.localEulerAngles.z + " : " + (roulette.localEulerAngles.z / 60));
                resultText.text = color + "が当たりました";
            }
        }
    }

    public void Stop()
    {
        isStop = true;
        stopBtn.SetActive(false);
    }
}

いかさまルーレットソース

止めるところにiTweenを使用しています。

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

public class RouletteManager4 : MonoBehaviour {

    public Transform roulette;
    public GameObject stopBtn;
    public GameObject startBtn;
    public Text resultText;

    private int[] sizeList;
    private float nowSpeed = 0;
    private float MAX_SPEED = 360f;
    private bool isStart = false;
    private bool isStop = false;
    private readonly string[] RESULT_COLOR = new string[]{"ピンク","黄色","紫","緑","水色","赤"};
    private float resultRotate = -30;
    private float moveAngle = 0;
    private float stopTime = 0;

    void Awake()
    {
        stopBtn.SetActive(false);
        resultText.text = "";
    }

    public void RotateStart()
    {
        resultText.text = "";
        isStart = true;
        stopBtn.SetActive(false);
        startBtn.SetActive(false);
    }

    void Update()
    {
        if(isStart)
        {
            if(!isStop)
            {
                if(nowSpeed < MAX_SPEED)
                {
                    nowSpeed += 5f;
                }
                else
                {
                    stopBtn.SetActive(true);
                }
                roulette.localEulerAngles = new Vector3(0, 0, roulette.localEulerAngles.z + nowSpeed * Time.deltaTime);
            }
        }
    }

    public void ShowResult()
    {
        isStart = false;
        isStop = false;
        startBtn.SetActive(true);
        string color = RESULT_COLOR[Mathf.FloorToInt((roulette.localEulerAngles.z / 60))];
        resultText.text = color + "が当たりました";
    }

    public void Stop()
    {
        int nowAngle = Mathf.FloorToInt(roulette.localEulerAngles.z) % 360;
        moveAngle = 360 - resultRotate - nowAngle + 720;
        stopTime = moveAngle / MAX_SPEED * 2;
        isStop = true;
        stopBtn.SetActive(false);
        iTween.RotateTo(roulette.gameObject, iTween.Hash("z", roulette.localEulerAngles.z + moveAngle, "easeType", "easeOutQuad", "time", stopTime, "oncompletetarget", gameObject ,"oncomplete", "ShowResult", "islocal", true));
    }
}
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7