Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity3D等轴测平铺贴图的鼠标事件_C#_Unity3d_2d_Tile_Isometric - Fatal编程技术网

C# Unity3D等轴测平铺贴图的鼠标事件

C# Unity3D等轴测平铺贴图的鼠标事件,c#,unity3d,2d,tile,isometric,C#,Unity3d,2d,Tile,Isometric,我一直在阅读Unity3D中新的瓷砖地图系统。我已经成功地设置了网格->平铺贴图和平铺调色板。然而,现在我正在努力寻找最新的教程来处理这个新的平铺贴图系统的鼠标事件 我试图在鼠标位于互动程序上方时设置高亮显示,如果单击互动程序,我希望能够触发脚本和其他事件。但是,可用的在线教程没有讨论平铺贴图系统的鼠标事件,很少讨论等轴测平铺贴图 在等轴测平铺贴图上处理鼠标事件有什么好的最新教程吗?即使是一个简单的教程显示了一个悬停效果的瓷砖和一个“你好世界从瓷砖x.y”点击瓷砖,将是所有我真的需要去 这就是我

我一直在阅读Unity3D中新的瓷砖地图系统。我已经成功地设置了网格->平铺贴图和平铺调色板。然而,现在我正在努力寻找最新的教程来处理这个新的平铺贴图系统的鼠标事件

我试图在鼠标位于互动程序上方时设置高亮显示,如果单击互动程序,我希望能够触发脚本和其他事件。但是,可用的在线教程没有讨论平铺贴图系统的鼠标事件,很少讨论等轴测平铺贴图

在等轴测平铺贴图上处理鼠标事件有什么好的最新教程吗?即使是一个简单的教程显示了一个悬停效果的瓷砖和一个“你好世界从瓷砖x.y”点击瓷砖,将是所有我真的需要去

这就是我到目前为止所做的:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseManager : MonoBehaviour
{
    void Update()
    {
        Vector3 clickPosition = Vector3.one;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if(Physics.Raycast(ray, out hit))
        {
            clickPosition = hit.point;
        }
        Debug.Log(clickPosition);
    }
}

这应该让你开始:

using UnityEngine;
using UnityEngine.Tilemaps;

public class Test : MonoBehaviour {

   //You need references to to the Grid and the Tilemap
   Tilemap tm;
   Grid gd;

   void Start() {
       //This is probably not the best way to get references to
       //these objects but it works for this example
       gd = FindObjectOfType<Grid>();
       tm = FindObjectOfType<Tilemap>();
   }

   void Update() {

       if (Input.GetMouseButtonDown(0)) {
           Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
           Vector3Int posInt = gd.LocalToCell(pos);

           //Shows the cell reference for the grid
           Debug.Log(posInt);

           // Shows the name of the tile at the specified coordinates
           Debug.Log(tm.GetTile(posInt).name);
       }
   }
}
使用UnityEngine;
使用UnityEngine.Tilemaps;
公共课堂测试:单一行为{
//您需要对网格和Tilemap的引用
TileMapTM;
网格gd;
void Start(){
//这可能不是获取引用的最佳方式
//这些对象,但它适用于此示例
gd=FindObjectOfType();
tm=FindObjectOfType();
}
无效更新(){
if(Input.GetMouseButtonDown(0)){
Vector3 pos=Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int-posInt=gd.LocalToCell(pos);
//显示网格的单元格引用
Log(posInt);
//显示指定坐标处磁贴的名称
Log(tm.GetTile(posInt.name);
}
}
}
简而言之,获取对网格和tilemap的引用。使用ScreenToWorldPoint(Input.mousePosition)查找本地坐标。调用网格对象的LocalToCell方法,将局部坐标(Vector3)转换为单元坐标(Vector3Int)。将单元格坐标传递给Tilemap对象的GetTile方法以获取平铺(然后使用与平铺类关联的方法进行任何您想要的更改)


在这个例子中,我只是将上面的脚本附加到世界上的一个空游戏对象上。相反,将其附加到网格可能是有意义的。尽管如此,一般逻辑仍然保持不变。

这应该让您开始:

using UnityEngine;
using UnityEngine.Tilemaps;

public class Test : MonoBehaviour {

   //You need references to to the Grid and the Tilemap
   Tilemap tm;
   Grid gd;

   void Start() {
       //This is probably not the best way to get references to
       //these objects but it works for this example
       gd = FindObjectOfType<Grid>();
       tm = FindObjectOfType<Tilemap>();
   }

   void Update() {

       if (Input.GetMouseButtonDown(0)) {
           Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
           Vector3Int posInt = gd.LocalToCell(pos);

           //Shows the cell reference for the grid
           Debug.Log(posInt);

           // Shows the name of the tile at the specified coordinates
           Debug.Log(tm.GetTile(posInt).name);
       }
   }
}
使用UnityEngine;
使用UnityEngine.Tilemaps;
公共课堂测试:单一行为{
//您需要对网格和Tilemap的引用
TileMapTM;
网格gd;
void Start(){
//这可能不是获取引用的最佳方式
//这些对象,但它适用于此示例
gd=FindObjectOfType();
tm=FindObjectOfType();
}
无效更新(){
if(Input.GetMouseButtonDown(0)){
Vector3 pos=Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int-posInt=gd.LocalToCell(pos);
//显示网格的单元格引用
Log(posInt);
//显示指定坐标处磁贴的名称
Log(tm.GetTile(posInt.name);
}
}
}
简而言之,获取对网格和tilemap的引用。使用ScreenToWorldPoint(Input.mousePosition)查找本地坐标。调用网格对象的LocalToCell方法,将局部坐标(Vector3)转换为单元坐标(Vector3Int)。将单元格坐标传递给Tilemap对象的GetTile方法以获取平铺(然后使用与平铺类关联的方法进行任何您想要的更改)


在这个例子中,我只是将上面的脚本附加到世界上的一个空游戏对象上。相反,将其附加到网格可能是有意义的。尽管如此,一般的逻辑仍然是一样的。

这与HumanWrites的方式略有不同。它不需要对网格的引用,并且鼠标点被声明为Vector2,而不是Vector3-这将避免在2D中工作时出现问题

using UnityEngine;
using UnityEngine.Tilemaps;

public class MouseManager : MonoBehaviour
{

private Tilemap tilemap;

void Start()
{
    tilemap = FindObjectOfType<Tilemap>();
}

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector3Int gridPos = tilemap.WorldToCell(mousePos);

        if (tilemap.HasTile(gridPos))
            Debug.Log("Hello World from " + gridPos);
    }
}

}
使用UnityEngine;
使用UnityEngine.Tilemaps;
公共类鼠标管理员:单行为
{
私人Tilemap Tilemap;
void Start()
{
tilemap=FindObjectOfType();
}
无效更新()
{
if(Input.GetMouseButtonDown(0))
{
Vector2 mousePos=Camera.main.ScreenToWorldPoint(Input.mousePosition);
向量3int gridPos=tilemap.WorldToCell(鼠标点);
if(tilemap.HasTile(gridPos))
Log(“helloworld from”+gridPos);
}
}
}
我们正在引用的“tilemap”是场景中的游戏对象。您可能已将其重命名为其他名称,但它将是“网格”对象的子对象。

这与HumanWrites的编写方式略有不同。它不需要对网格的引用,并且鼠标点被声明为Vector2,而不是Vector3-这将避免在2D中工作时出现问题

using UnityEngine;
using UnityEngine.Tilemaps;

public class MouseManager : MonoBehaviour
{

private Tilemap tilemap;

void Start()
{
    tilemap = FindObjectOfType<Tilemap>();
}

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector3Int gridPos = tilemap.WorldToCell(mousePos);

        if (tilemap.HasTile(gridPos))
            Debug.Log("Hello World from " + gridPos);
    }
}

}
使用UnityEngine;
使用UnityEngine.Tilemaps;
公共类鼠标管理员:单行为
{
私人Tilemap Tilemap;
void Start()
{
tilemap=FindObjectOfType();
}
无效更新()
{
if(Input.GetMouseButtonDown(0))
{
Vector2 mousePos=Camera.main.ScreenToWorldPoint(Input.mousePosition);
向量3int gridPos=tilemap.WorldToCell(鼠标点);
if(tilemap.HasTile(gridPos))
Log(“helloworld from”+gridPos);
}
}
}
我们正在引用的“tilemap”是场景中的游戏对象。您可能已将其重命名为其他名称,但它将是“网格”对象的子对象。

对于节。查找(“Tilemap”)是查找组件名称还是标签?对于节。查找(“Tilemap”)是查找组件名称还是标签?