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', data):
    tag = re.search(r', 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)

你可能感兴趣的:(unity,游戏引擎)