Skip to content

Instantly share code, notes, and snippets.

@nabesi777
Created September 17, 2018 10:49
Show Gist options
  • Save nabesi777/10a1b90e5d10eaa2ab80f805c8faf9ca to your computer and use it in GitHub Desktop.
Save nabesi777/10a1b90e5d10eaa2ab80f805c8faf9ca to your computer and use it in GitHub Desktop.
ToggleButton C# Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ToggleButton : MonoBehaviour
{
public TextMesh textMesh; //3DTextを格納
public Texture2D button; //buttonイメージを格納
public GameObject spider; //蜘蛛オブジェクトを格納
private bool spiderOn = false; //spiderOnはfalse
private double sum;
public int valueOne = 5;
public int valueTwo = 10;
private string buttonOneDisplay;
private string buttonTwoDisplay;
private string valueOneString;
private string valueTwoString;
private bool muteToggle = false;  //基本状態はmuteボタンはfalse
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
valueOneString = valueOne.ToString();
valueTwoString = valueTwo.ToString();
buttonOneDisplay = "Display" + valueOneString;
buttonTwoDisplay = "Display" + valueTwoString;
sum = valueOne + valueTwo;
}
void OnGUI()
{                          //カーソルがボタン上にある時にTooltipsとして””内が表示
if (GUI.Button(new Rect(10, 70, 100, 20), new GUIContent(buttonOneDisplay, "Number 1")))
{
textMesh.text = valueOne.ToString(); //テキストへはこちらが出力
}
//カーソルがボタン上にある時にTooltipsとして””内が表示
if (GUI.Button(new Rect(10, 95, 100, 20), new GUIContent(buttonTwoDisplay, "Number 2")))
{
textMesh.text = valueTwo.ToString(); //テキストへはこちらが出力
}
//ボタンへの表示は"Spider Button"、Tooltipsの表示は"Don't press that button"
if (GUI.Button(new Rect(10, 120, 100, 20), new GUIContent("Spider Button", "Don't press that button")))
{
if (spiderOn == false)
{
spider.SetActive(true);
spiderOn = true;
}
else
{
spider.SetActive(false);
spiderOn = false;
}
}
//sumボタン
if (GUI.Button(new Rect(10, 145, 100, 20), new GUIContent("sum", "合計します")))
{
textMesh.text = sum.ToString(); //39行目 sumを出力
}
//muteボタン    ボタンを付ける( 位置 |大きさ ) ボタンへ表示
muteToggle = GUI.Toggle(new Rect(130, 145, 100, 50), muteToggle, "mute");
if(muteToggle==false)
{
AudioListener.volume = 0.3f;
}
else if(muteToggle==true)  //muteボタンが押されたらボリュームがゼロに
{
AudioListener.volume = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment