- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当用户单击并按住鼠标按钮(拖动鼠标)时,我想使用鼠标位置创建一个预制件 - (类似于油漆中的画笔绘图 - 除了用预制件更改画笔标记)。
当鼠标缓慢移动时,效果很好。我遇到的问题是,如果用户移动鼠标太快,“更新”功能不会记录所有鼠标位置,并且每个预制件的位置都离前一个预制件很远。
初始代码:
public class Draw : MonoBehaviour
{
public GameObject Brush;
private bool _isDraging = false;
private Vector3 _mousePos;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
_isDraging = true;
}
if (Input.GetMouseButtonUp(0))
{
_isDraging = false;
return;
}
if (_isDraging)
{
_mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
_mousePos.z = 0;
//I have added 'Physics.OverlapBox' so that the prefab doesn't overlap with the previous prefab
var collide = Physics.OverlapBox(_mousePos, new Vector3(0, 0, 0));
if (!collide.Any())
{
Debug.Log("Draw: " + _mousePos);
Instantiate(Brush, _mousePos, Quaternion.identity);
}
}
}
}
更新:
void Update()
{
//I use this to set the initial mouse position
if (Input.GetMouseButtonDown(0))
{
_lastPlacedObjectLocation = _get2dMousePosition();
//_isDraging is now removed
}
if (Input.GetMouseButton(0))
{
var currentMousePosition = _get2dMousePosition();
var distanceTravelled = _lastPlacedObjectLocation - currentMousePosition;
var stepDistance = distanceTravelled.magnitude;
//I kept 1f - this is the distance between the prefabs - also my prefab size. Not sure if this is the best approach (the only way I could get it to work)
for (float i = 0; i < stepDistance; i += 1f)
{
float progress = i / stepDistance;
var placementPosition = Vector3.Lerp(_lastPlacedObjectLocation, currentMousePosition, progress);
//Removed Physics.OverlapBox - in some places we want an overlap otherwise there is a gap between the prefabs
Instantiate(Brush, placementPosition, Quaternion.identity);
}
_lastPlacedObjectLocation = _get2dMousePosition();
}
}
最佳答案
既然你已经注意到,你的鼠标速度是可变的,而更新率不是(它可以是可变的,也可以是固定的。无论如何,它与光标的移动无关),这意味着你的鼠标光标可以在两个更新调用之间移动相当远的距离。
该问题最简单的解决方案是遍历最后已知的鼠标位置与当前位置之间的距离,并填充所有可用空间。
我还没有为你彻底解决问题;有改进的余地,我现在无法真正测试它,但这应该会让您朝着正确的方向前进。
public class Draw : MonoBehaviour
{
public GameObject Brush;
private bool _isDraging = false;
//we are going to keep track of the location where we last placed an object.
private Vector3 _lastPlacedObjectLocation;
private Vector3 _get2dMousePosition()
{
Vector3 newPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
newPos.z = 0;
return newPos;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
_isDraging = true;
}
if (Input.GetMouseButtonUp(0))
{
_isDraging = false;
//keep 'old' mouse position up to date, so we do not draw in between dragging the mouse and releasing it.
_lastPlacedObjectLocation = _get2dMousePosition();
return;
}
if (_isDraging)
{
Vector3 currentMousePosition = _get2dMousePosition();
//now, we find how far the mouse has moved since the last step.
Vector3 distanceTravelled = _lastPlacedObjectLocation - currentMousePosition;
//since we know the starting and ending point, all we have to do now is add the prefab instances, neatly spaced over the travelled distance.
Vector3 stepDistance = distanceTravelled / spacing;
//then, we interpolate between the start and end vector
//you could make this a lot faster by determining how often your object fits in this space, but i'd like to leave you a little bit of a challenge, so i'm abusing the magnitude of the stepDistance ;)
for(float i =0; i < stepDistance.magnitude; i+=0.1f)
{
float progress = i / stepDistance.magnitude;
Vector3 placementPosition = Vector3.Lerp(_lastPlacedObjectLocation, currentMousePosition, progress);
placePrefab(placementPosition);
}
//and then we update the last-placed-object location
_lastPlacedObjectLocation = _get2dMousePosition();
}
}
void placePrefab(Vector3 location)
{
//I have added 'Physics.OverlapBox' so that the prefab doesn't overlap with the previous prefab
var collide = Physics.OverlapBox(location, new Vector3(0, 0, 0));
if (!collide.Any())
{
Debug.Log("Draw: " + location);
Instantiate(Brush, location, Quaternion.identity);
}
}
}
关于c# - 在 Unity 中通过鼠标拖动创建预制件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44452780/
我正在使用 gridstack 来显示我的小部件。拖动和调整大小适用于 gridstack 卡,但当我将卡拖到底部时,卡的容器不会滚动。我想在拖动卡片时滚动那个容器。该容器只是一个 div 元素,所有
我目前正在构建一个多个处理的 slider 小部件,并且目前正在实现手势检测器。我有一个问题,如果您用第二根手指触摸/拖动屏幕,检测器会识别它并调用 onDragUpdate 函数,这是我试图禁用的功
我是 AngularJS 的新手,我对当前的项目有点压力,这就是我在这里问的原因。 我看到 AngularJS 有 ng-dragstart 事件( http://docs.ng.dart.ant.c
关于缩放大图像和平移有什么建议吗?最好内联在页面上。 我一直在使用PanoJS(又名GSV2),但是现在越来越多的人使用iPhone/iPad/Android类型的设备,这个库要么太慢,要么旧版本不支
我在尝试拖动 JPanel 时遇到问题。如果我纯粹在 MouseDragged 中将其实现为: public void mouseDragged(MouseEvent me) { me.getS
我有一个需要与屏幕底部对齐的 ImageButton,当我单击并拖动它时,它会随着我的手指移动一定距离,超过该距离它不会远离原点,而是继续跟随我的手指从原点开始的方向。 当我松手时,按钮必须滑回原点。
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this qu
我需要一个解决方案来实现拖动和缩放,在不使用矩阵的情况下围绕屏幕 onTouch 事件路由 ImageView。我搜索了很多但所有答案都是用矩阵完成的,因为我在屏幕矩阵中有多个对象不适合我,我想拖动和
我需要一些帮助来拖动 UIView 以显示主视图下方的菜单 View 。 我有两个 UIView。menuView - 包括菜单按钮和标签 和 mainView - 位于 menuView 之上。 我
谷歌地图即使设为 true 也不可拖动/拖动。 重现步骤:在新选项卡中加载带有 map 的页面,检查并在不执行任何操作的情况下转到移动 View 。现在在移动 View 中左右上下拖动 map ,它将
我在绘制和缩放 ImageView 时遇到问题。请帮帮我.. 当我画一些东西然后拖动或缩放图像时 - 绘图保留在原处,如您在屏幕截图中所见。而且我只需要简单地在图片上绘图,并且可以缩放和拖动这张图片。
所以我试图模拟鼠标左键单击和鼠标左键释放来执行一些自动拖放操作。 它目前在 C# Winforms(是的,winforms :|)中并且有点笨拙。 基本上,一旦发送了点击,我希望它根据 Kinect
我有一个巨大的 HTML5 Canvas,我希望它像谷歌地图一样工作:用户可以拖动它并始终只看到它的一小部分(屏幕大小)。它只呈现您在屏幕上可以看到的部分。我该怎么做?你有想法吗? 最佳答案 2 个简
我有以下拖放应用程序代码,它在桌面上按预期工作,但是当我想在移动设备上使用该应用程序时,拖动事件无法按预期工作。我知道触摸事件是必需的,但我不确定如何设置它们和实现这些功能。 .object
我正在使用 react-leaflet map ,我用状态改变了很多 map 属性,但是当使用状态来改变拖动属性时它不起作用。 { this.map = map}}
我知道我可以轻松地允许用户在OpenLayers中选择多个功能/几何图形,但是随后我希望使用户能够轻松地同时拖动/移动所有选定的功能。 使用ModifyFeature控件一次只能移动一个要素...是否
我有一个 Windows Phone 运行时应用程序,我使用 xaml 在 map 上显示图钉。 当我拖动 map 时,控件试图保持在相同位置时会出现一些滞后。 任何帮助,将不胜感
我通常不会提出此类问题/答案,但我想我会这样做,因为我已经看到这个问题被问了 20 多次,但没有一个答案真正有效。简而言之,问题是,如果您在可拖动 jQuery 项目内的任何位置有可滚动内容(over
我确信一定有一种简单的方法可以做到这一点,但到目前为止,我已经花了很长时间在各种兔子洞中没有成功。 我有一个支持拖放的 Collection View 。被拖动的单元格有 UIImageView在 c
如何在两个virtualtreeview之间复制以复制所有列,而不仅仅是第一列? 复制前: 复制后: 最佳答案 树控件不保存任何数据。它不包含要显示的列数据,因此无法复制它。而是,当树控件想要显示任何
我是一名优秀的程序员,十分优秀!