using System.Collections;
using System.Collections.Generic;
public class KuruKuruMenu : MonoBehaviour {
public GameObject mainManu; // メインのメニューボタンGameObject
public Transform aroundMenuParentTran; // サブメニューの親のTransform
public GameObject uiCamera; // menuを移しているカメラ
private GameObject startAroundMenu; // 一つ目の子のサブメニュー。これの位置で現在の回転角度を算出
private int subMenuCount = 0; // サブメニューの数
private bool isSubMenu; // サブメニューが表示されているかどうか
private bool isRotateMenu; // サブメニューが開店中かどうか
private float menuRotation; // 1つ目のサブメニューの回転角度
private float radius = 150; // サブメニューの半径
foreach(Transform tran in aroundMenuParentTran)
startAroundMenu = tran.gameObject;
// Use this for initialization
// Update is called once per frame
if(Input.GetMouseButtonDown(0) && isSubMenu && !isRotateMenu)
RaycastHit2D hit2d = Physics2D.Raycast(uiCamera.GetComponent<Camera>().ScreenToWorldPoint(Input.mousePosition), Vector3.forward);
// 自分以外がタッチされたら閉じる 自分だった場合は回転などの処理
if (hit2d.collider != null)
if(hit2d.collider.name == "AroundMenuParent" || hit2d.collider.name.IndexOf("SubMenu") != -1)
if((hit2d.collider.name == "AroundMenuParent" && hit2d.collider.transform == aroundMenuParentTran) ||
(hit2d.collider.name.IndexOf("SubMenu") != -1 && hit2d.collider.transform.parent == aroundMenuParentTran)
StartCoroutine(MoveSubMenu());
else if(hit2d.collider.name == "MainMenu")
if(hit2d.collider.gameObject != mainManu)
private IEnumerator MoveSubMenu()
menuRotation = GetRotation(aroundMenuParentTran.position, startAroundMenu.transform.position);
Vector3 moveStartPos = uiCamera.GetComponent<Camera>().ScreenToWorldPoint(Input.mousePosition);
float moveStartRotation = GetRotation(aroundMenuParentTran.position, moveStartPos);
Vector3 nowMousePos = Vector3.zero; // 現在のマウスの位置
float nowRotation = 0; // 現在のマウスの角度
float moveRotation = 0; // 動かした角度
float prevRotation = 0; // 前の角度
float delta = 0; // 動かす角度
while(Input.GetMouseButton(0))
nowMousePos = uiCamera.GetComponent<Camera>().ScreenToWorldPoint(Input.mousePosition);
nowRotation = GetRotation(aroundMenuParentTran.position, nowMousePos);
moveRotation = nowRotation - moveStartRotation;
RotateSubMenu(moveRotation);
delta = nowRotation - prevRotation;
prevRotation = nowRotation;
menuRotation = GetRotation(aroundMenuParentTran.position, startAroundMenu.transform.position);
if(delta > 20) delta = 20;
else if(delta < -20) delta = -20;
while(!Input.GetMouseButton(0) && delta != 0)
if(Mathf.Abs(delta) < 0.01f) delta = 0;
menuRotation = GetRotation(aroundMenuParentTran.position, startAroundMenu.transform.position);
private void RotateSubMenu(float delta)
Vector3 pos = Vector3.zero; // サブメニューの位置
foreach(Transform tran in aroundMenuParentTran)
rot = 360 / subMenuCount * i + 90 + delta + menuRotation;
rad = rot * Mathf.Deg2Rad; // 角度からラジアンに変換
pos.x = Mathf.Cos(rad) * radius;
pos.y = Mathf.Sin(rad) * radius;
tran.localPosition = pos;
// basePosからtargetPosまでの角度を返す
private float GetRotation(Vector3 basePos, Vector3 targetPos)
float dx = targetPos.x - basePos.x;
float dy = targetPos.y - basePos.y;
float rad = Mathf.Atan2(dy, dx);
float rotation = rad * Mathf.Rad2Deg - 90;
public void MainMenuClick()
aroundMenuParentTran.GetComponent<TweenScale>().PlayReverse();
aroundMenuParentTran.GetComponent<TweenScale>().PlayForward();