制作一個類似與這種格式的菜單,可以伸縮滑動的。
今天正好項目需要用到類似功能,所以嘗試了一下,做出如下的效果
雖然只是一個思路,但是可以擴展。
聲明一個object物體,為but,通過GetComponent<RectTransform>().anchoredPosition,將其賦值移動到目標位置
下面是UGUI的cs代碼。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class UGUI : MonoBehaviour {
public Image image;
public bool image_bool;
public GameObject but;
// Use this for initialization
void Start () {
image_bool = false;
}
// Update is called once per frame
void Update () {
}
public void button()
{
image_bool = !image_bool;
image.gameObject.SetActive(image_bool);
but.GetComponent<RectTransform>().anchoredPosition = new Vector2(27, -85);
}
}
這邊button代碼
下面注釋部分是采用碰撞box collider2D,獲取imgage,和下面button(1)碰撞事件,獲取rectTransform.anchoredPosition相對的坐標,然后將其擠開,從而實現移動的效果,有興趣的可以試試下面這種,需要在image和button(1)添加box collider2D盒子碰撞器以及rigidbody 2D事件,另外千萬不要忘記了Constranints里面的 freeze position 將x 和y勾選上噢。
下面是完整代碼,其余綁定事件什么的我就不貼出來了阿,可以自行網上參考。
using UnityEngine;
這邊button代碼
下面注釋部分是采用碰撞box collider2D,獲取imgage,和下面button(1)碰撞事件,獲取rectTransform.anchoredPosition相對的坐標,然后將其擠開,從而實現移動的效果,有興趣的可以試試下面這種,需要在image和button(1)添加box collider2D盒子碰撞器以及rigidbody 2D事件,另外千萬不要忘記了Constranints里面的 freeze position 將x 和y勾選上噢。

下面是完整代碼,其余綁定事件什么的我就不貼出來了阿,可以自行網上參考。
using UnityEngine;
using System.Collections;
public class button : MonoBehaviour {
public Vector2 zi_v2;
public bool ziji_bool;
// Use this for initialization
void Start () {
ziji_bool = false;
zi_v2 = new Vector2(GetComponent<RectTransform>().anchoredPosition.x,GetComponent<RectTransform>().anchoredPosition.y);
}
// Update is called once per frame
void Update () {
if (!GameObject.Find("Canvas").GetComponent<UGUI>().image_bool) {
hf();
}
}
public void hf() {
GetComponent<RectTransform>().anchoredPosition = zi_v2;
}
//void OnCollisionEnter2D(Collision2D coll)
//{
// Debug.Log(coll.gameObject.name);
// RectTransform coll_name = GameObject.Find(coll.gameObject.name).GetComponent<RectTransform>();
// //GetComponent<RectTransform>().anchoredPosition = new Vector2(GetComponent<RectTransform>().anchoredPosition.x, coll_name.sizeDelta.y);
// GetComponent<RectTransform>().anchoredPosition = new Vector2(GetComponent<RectTransform>().anchoredPosition.x, GetComponent<RectTransform>().anchoredPosition.y);
// ziji_bool = true;
//}
}