unity射线无法检测到uidocment (附uxml转csharp python 实现)

ui功能需求

  • 组件复用
  • 动态修改
  • 是否免疫射线检测
  • 方便修改

问题

!Physics.Raycast(new Vector3(newPos.x, -7, newPos.y),  new Vector3(0, 1, 0), out hit,  20f);

无法检测到ui

目前解决方法(就是避开这个问题)

编写脚本HandlerClickScript继承MonoBehaviour实现OnMouseDown()

using UnityEngine;
public class HandlerClickScript : MonoBehaviour
{
    public delegate void MouseClickedEvent(string tag, Vector3 pos);
    public static event MouseClickedEvent mouseClickedEvent;
    private void OnMouseDown()
    {
        mouseClickedEvent(gameObject.tag, gameObject.transform.position);
    }
}

然后给每个物体加上

附uxml转csharp python 实现

  • 能处理uxml嵌套…但需要手动修改一点
import re

from_path = r"F:\Unity Projects\learn1.0\New Unity Project\Assets\Classic\UIDocuments\SuitangUiDoc.uxml"
to_path = ""
class_name = "Component"


with open(from_path, "r", encoding="utf-8") as f:
    data = f.read()


d2 = []
rlt  = """
public class """+class_name+"""
{
    public TemplateContainer root;
"""

rlt2 = """

    public """+class_name+"""(TemplateContainer root){
        this.root = root;
        
"""

for i in re.findall(r'<ui:.*name=".*?"', data):
    tag = re.search(r'<ui:.*? ', i).group()[4:].replace(" ", "")
    _id = re.search(r'name=".*?"', i).group()[6: -1].replace(" ", "")

    if tag == "Template":
        continue
    if tag == "Instance":
        tag = re.search(r'template=".*?"', i).group()[10:-1].replace(" ", "")

    _id2 = _id[0].lower() + _id[1:]
    rlt += f"\tpublic {tag} {_id2};\n"
    rlt2 += f"\t\t{_id2} = root.Q<{tag}>(\"{_id}\");\n"
    print(tag, _id)

rlt2 += "\t}\n"

rlt += rlt2 + "}\n"

print(rlt)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值