unity3d 不规则按钮

unity3d 4.3后添加了很多2D控件,这就大大方便UI的制作,恰好NGUI不能做到对2D不规则按钮的检测,只能用UGUI了

把图片改成Sprite  

然后把material放到sprite指定位置

用按钮图片做一个Material


附加在按钮上的脚本里面

using UnityEngine;
using System.Collections;

public class Button : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    void OnMouseDown()
    {
        Debug.Log("hei");
    }
}

这样点击图片就会打印hei

如果你同时使用NGUI做UI肯能会出现按钮无法显示,或无法触发的情况。这时你可以再创建一个相机让该相机与按钮处于相同的layer层,

通过调整相机参数是按钮正常显示,此时不规则按钮就可以正常使用了。   欢迎留言讨论

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将MySQL数据库与Unity3D中的按钮控件结合使用,您需要编写一些代码来实现以下步骤: 1. 建立数据库连接:您需要使用MySQL连接器来建立Unity3D和MySQL数据库之间的连接。您可以在Unity Asset Store中搜索MySQL连接器,并将其导入到您的Unity项目中。 2. 查询数据库:一旦建立了连接,您可以使用SQL查询语句从数据库中检索数据。例如,您可以使用SELECT语句检索按钮的标签和位置。 3. 更新按钮控件:一旦您检索到按钮的标签和位置,您可以使用Unity按钮控件来更新按钮的标签和位置。例如,您可以使用button.GetComponentInChildren<Text>()方法获取按钮的文本组件,并使用它来设置按钮的标签。 以下是一个简单的示例代码,演示如何将MySQL数据库与Unity3D中的按钮结合使用: ```c# using UnityEngine; using System.Collections; using System.Data; using MySql.Data.MySqlClient; public class ButtonController : MonoBehaviour { public GameObject buttonPrefab; public Transform buttonContainer; private MySqlConnection connection; private MySqlCommand command; private MySqlDataReader reader; void Start () { // Connect to the database string connectionString = "Server=localhost;Database=mydatabase;Uid=root;Pwd=;"; connection = new MySqlConnection(connectionString); connection.Open(); // Query the database for button data string query = "SELECT * FROM buttons"; command = new MySqlCommand(query, connection); reader = command.ExecuteReader(); // Create buttons based on the data retrieved from the database while (reader.Read()) { // Get the button label and position from the database string label = reader.GetString("label"); Vector3 position = new Vector3(reader.GetFloat("x"), reader.GetFloat("y"), reader.GetFloat("z")); // Create a new button and set its label and position GameObject button = Instantiate(buttonPrefab, position, Quaternion.identity) as GameObject; button.transform.SetParent(buttonContainer); button.GetComponentInChildren<Text>().text = label; } // Close the database connection connection.Close(); } } ``` 请注意,这只是一个示例代码,您需要根据您的具体需求进行修改,并确保您的MySQL数据库已正确设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值