【发布时间】:2022-01-18 15:26:40
【问题描述】:
我还是 C# 的新手,但我希望我可以理解地提出我的问题:使用以下代码,我在 Unity 中每次单击鼠标时都会在纹理上绘制两个随机点。我现在想在每次鼠标点击时用一条线连接这些点,无论这些点在哪里。我已经尝试了一些事情并遇到了 Graphics.DrawLine() 方法。但是,这对我不起作用,根据我的研究,这很可能是因为该项目正在运行并且应该继续在 Mac 上运行。
我希望你能帮助我解决这个问题,也许知道这也适用于 macOS。
如果有任何问题,请随时问我。
谢谢! 拉拉
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Texture2DEditor : MonoBehaviour
{
Texture2D t2D;
void Start()
{
var rawImage = GetComponent<RawImage> ();
t2D = rawImage.texture as Texture2D;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector2Int point1 = new Vector2Int(Random.Range(0,1072),Random.Range(0,1072));
Vector2Int point2 = new Vector2Int(Random.Range(0,1072),Random.Range(0,1072));
Vector2Int line = point2 - point1;
int x1 = (int) line.x;
int x2 = (int) line.x;
int y1 = (int) line.y;
int y2 = (int) line.y;
t2D.SetPixel(x1,y1,new Color(255,255,255));
t2D.SetPixel(x2,y2,new Color(255,255,255));
}
t2D.Apply();
}
}
【问题讨论】:
-
我想我会从 Get all pixels between two points 开始(它在 javascript 中,但在任何语言中的概念都是一样的)