11233人加入学习
(25人评价)
Unity研究实验室
价格 免费
using System.Collections.Generic;
using UnityEngine;

public class Mspaint : MonoBehaviour
{
    private Color paintColor = Color.red;
    private float paintSize = 0.1f;
    private LineRenderer currentLine;
    public Material lineMaterial;
    private List<Vector3> positions = new List<Vector3>();
    private bool isMouseDown = false;
    private Vector3 lastMousePosition = Vector3.zero;
    private float lineDistance = 0.02f;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))// 2是鼠标滑轮,0是鼠标左键,1是鼠标右键
        {
            //创建空物体,添加LineRenderer并设置基础属性
            GameObject go = new GameObject();
            go.transform.SetParent(this.transform);
            currentLine = go.AddComponent<LineRenderer>();
            currentLine.material = lineMaterial;
            currentLine.startWidth = paintSize;
            currentLine.endWidth = paintSize;
            currentLine.startColor = paintColor;
            currentLine.endColor = paintColor;

            //使得LineRenderer线平滑,起始点圆滑,拐角点圆滑
            currentLine.numCornerVertices = 5;
            currentLine.numCapVertices = 5;

            //给LineRenderer设置开始位置
            Vector3 position = GetMousePoint();
            AddPosition(position);

            isMouseDown = true;

            lineDistance += 0.02f; //使得后画线更靠近相机,实现后画点覆盖前面画的点
        }

        if (isMouseDown)
        {
            Vector3 position = GetMousePoint();
            //性能优化:移动距离大些,才添加点
            if (Vector3.Distance(position, lastMousePosition) > 0.1f)
                AddPosition(position);
        }

        if (Input.GetMouseButtonUp(0))
        {
            currentLine = null;
            positions.Clear();
            isMouseDown = false;
        }
    }

    void AddPosition(Vector3 position)
    {
        //使得线靠近相机,不被Plane重叠一起,,使得后画线更靠近相机,实现后画点覆盖前面画的点
        position.z -= lineDistance;
        positions.Add(position);
        currentLine.positionCount = positions.Count; //currentLine.numPositions = positions.Count; // numPositions 已弃用,使用positionCount代替
        currentLine.SetPositions(positions.ToArray());
        //记录上次添加的点
        lastMousePosition = position;
    }

    Vector3 GetMousePoint()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        bool isCollider = Physics.Raycast(ray, out hit);
        if (isCollider)
        {
            return hit.point;
        }
        return Vector3.zero;
    }

    #region onvaluechanged method
    public void OnRedColorChanged(bool isOn)
    {
        if (isOn)
            paintColor = Color.red;

    }

    public void OnGreenColorChanged(bool isOn)
    {
        if (isOn)
            paintColor = Color.green;

    }

    public void OnBlueColorChanged(bool isOn)
    {
        if (isOn)
            paintColor = Color.blue;

    }

    public void OnPoint1SizeChanged(bool isOn)
    {
        if (isOn)
            paintSize = 0.1f;

    }

    public void OnPoint2SizeChanged(bool isOn)
    {
        if (isOn)
            paintSize = 0.2f;

    }

    public void OnPoint4SizeChanged(bool isOn)
    {
        if (isOn)
            paintSize = 0.4f;
    }
    #endregion
}

 

[展开全文]
遗失的星空 · 2021-03-24 · 该任务已被删除 0
using System.Collections.Generic;
using UnityEngine;

public class Mspaint : MonoBehaviour
{
    private Color paintColor = Color.red;
    private float paintSize = 0.1f;
    private LineRenderer currentLine;
    public Material lineMaterial;
    private List<Vector3> positions = new List<Vector3>();
    private bool isMouseDown = false;

    void Update()
    {
        // 2是鼠标滑轮,0是鼠标左键,1是鼠标右键
        if (Input.GetMouseButtonDown(0))
        {
            //创建空物体,添加LineRenderer并设置基础属性
            GameObject go = new GameObject();
            go.transform.SetParent(this.transform);
            currentLine = go.AddComponent<LineRenderer>();
            currentLine.material = lineMaterial;
            currentLine.startWidth = paintSize;
            currentLine.endWidth = paintSize;
            currentLine.startColor = paintColor;
            currentLine.endColor = paintColor;

            //使得LineRenderer线平滑,起始点圆滑,拐角点圆滑
            currentLine.numCornerVertices = 5;
            currentLine.numCapVertices = 5;

            //给LineRenderer设置开始位置
            AddPosition();

            isMouseDown = true;
        }
        if (isMouseDown)
        {
            AddPosition();
        }
        if (Input.GetMouseButtonUp(0))
        {
            currentLine = null;
            positions.Clear();
            isMouseDown = false;
        }
    }

    void AddPosition()
    {
        Vector3 position = GetMousePoint();
        //使得线靠近相机,不被Plane重叠一起
        position.z -= 0.1f;
        positions.Add(position);
        //currentLine.numPositions = positions.Count; // numPositions 已弃用,使用positionCount代替
        currentLine.positionCount = positions.Count;
        currentLine.SetPositions(positions.ToArray());
    }

    Vector3 GetMousePoint()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        bool isCollider = Physics.Raycast(ray, out hit);
        if (isCollider)
        {
            return hit.point;
        }
        return Vector3.zero;
    }

    #region onvaluechanged method
    public void OnRedColorChanged(bool isOn)
    {
        if (isOn)
        {
            paintColor = Color.red;
        }
    }

    public void OnGreenColorChanged(bool isOn)
    {
        if (isOn)
        {
            paintColor = Color.green;
        }
    }

    public void OnBlueColorChanged(bool isOn)
    {
        if (isOn)
        {
            paintColor = Color.blue;
        }
    }

    public void OnPoint1SizeChanged(bool isOn)
    {
        if (isOn)
        {
            paintSize = 0.1f;
        }
    }

    public void OnPoint2SizeChanged(bool isOn)
    {
        if (isOn)
        {
            paintSize = 0.2f;
        }
    }

    public void OnPoint4SizeChanged(bool isOn)
    {
        if (isOn)
        {
            paintSize = 0.4f;
        }
    }
    #endregion
}

 

[展开全文]
遗失的星空 · 2021-03-24 · 该任务已被删除 0
using UnityEngine;

public class Mspaint : MonoBehaviour
{
    private Color paintColor = Color.red;
    private float paintSize = 0.1f;

    void Update()
    {
        // 2是鼠标滑轮,0是鼠标左键,1是鼠标右键
        if (Input.GetMouseButtonDown(1))
        {

        }
        if (Input.GetMouseButtonUp(1))
        {

        }
    }

    #region onvaluechanged method
    public void OnRedColorChanged(bool isOn)
    {
        if (isOn)
        {
            paintColor = Color.red;
        }
    }

    public void OnGreenColorChanged(bool isOn)
    {
        if (isOn)
        {
            paintColor = Color.green;
        }
    }

    public void OnBlueColorChanged(bool isOn)
    {
        if (isOn)
        {
            paintColor = Color.blue;
        }
    }

    public void OnPoint1SizeChanged(bool isOn)
    {
        if (isOn)
        {
            paintSize = 0.1f;
        }
    }

    public void OnPoint2SizeChanged(bool isOn)
    {
        if (isOn)
        {
            paintSize = 0.2f;
        }
    }

    public void OnPoint4SizeChanged(bool isOn)
    {
        if (isOn)
        {
            paintSize = 0.4f;
        }
    }
    #endregion
}

 

[展开全文]
遗失的星空 · 2021-03-24 · 该任务已被删除 0

Mspaint.cs

using UnityEngine;

public class Mspaint : MonoBehaviour
{
    private Color paintColor = Color.red;
    private float paintSize = 0.1f;

    #region onvaluechanged method
    public void OnRedColorChanged(bool isOn)
    {
        if (isOn)
        {
            paintColor = Color.red;
        }
    }

    public void OnGreenColorChanged(bool isOn)
    {
        if (isOn)
        {
            paintColor = Color.green;
        }
    }

    public void OnBlueColorChanged(bool isOn)
    {
        if (isOn)
        {
            paintColor = Color.blue;
        }
    }

    public void OnPoint1SizeChanged(bool isOn)
    {
        if (isOn)
        {
            paintSize = 0.1f;
        }
    }

    public void OnPoint2SizeChanged(bool isOn)
    {
        if (isOn)
        {
            paintSize = 0.2f;
        }
    }

    public void OnPoint4SizeChanged(bool isOn)
    {
        if (isOn)
        {
            paintSize = 0.4f;
        }
    }
    #endregion
}

注册动态事件:(dynamic bool下的方法)

[展开全文]
遗失的星空 · 2021-03-24 · 该任务已被删除 0

ToggleGroup组件---OnValueChanged事件

[展开全文]
遗失的星空 · 2021-03-24 · 该任务已被删除 0

Use World Space

Positions:

Width:

Color:需要设置材质及Shader,Shader设置为Sprites/Default可以显示,拖动出外面删掉Color里面的点【注意:紫色说明缺少材质Material或者Shader代码有问题】

Corner Vertices:拐角点的数量

End Cap Vertices:两端点的数量

[展开全文]
遗失的星空 · 2021-03-23 · 该任务已被删除 0

考虑性能。

[展开全文]
Killernot · 2020-05-14 · 该任务已被删除 0

添加组件。

每写一点代码就测试一下。

默认空物体linerender有两个点。

给material . 

设置。linerender的numpostion.

重复的地方可以做成一个方法。

numCornerVertices 

numCapverticles 

 

 

[展开全文]
Killernot · 2020-05-14 · 该任务已被删除 0

鼠标得到世界坐标。

屏幕坐标映射到世界坐标。

利用鼠标射线于plane进行绘制。

if(Input.getmouseButtonDown(0))

绘制开始  创建linerender

中 保持绘制。

if(INput.getMouseButtonUp(0))

绘制结束

[展开全文]
Killernot · 2020-05-14 · 该任务已被删除 0

msapint 

ColorChange(bool isOn)

[展开全文]
Killernot · 2020-05-14 · 该任务已被删除 0

大小单选框。

[展开全文]
Killernot · 2020-05-14 · 该任务已被删除 0

材质里tint可以修改颜色

推荐使用linerender的颜色

颜色选择。

Toggle

UI 锁上。

255纯。

ToggleGroup。多选变单选。

ToggleGroup 经常看api文档。。经常看api文档。。。。。。。。。。。经常看api文档。。

 

[展开全文]
Killernot · 2020-05-14 · 该任务已被删除 0

linerender和自身transform没啥联系。

默认的两个postion。来实现的。

Position UseWordSpace 取消勾选就会进行影响了。

一般紫色,是出错的状态。

width宽度。

Color 不能影响。取消点拖到外边

Coner Vertices 角的落顶点数。点数越多,拐弯越平滑。

一个material加上去。有材质才能改变颜色。

更改shader。

 

[展开全文]
Killernot · 2021-04-06 · 该任务已被删除 0

画图。

lineRender

根据点来hua'xian

[展开全文]
Killernot · 2021-03-25 · 该任务已被删除 0

在代码中写入agent.updateposition=false;

agent.updaterotation=false;

那么这时navmeshagent就只负责寻路,而不会改变物体的坐标和转向了

还需要agent.nextposition=transform.position;不然模拟位置会和物体不在同一位置,导致以后的路径计算出问题

agent.remainingdistance返回一个当前物体离目标位置最短路径的剩余录成路程

agent.desiredvelocity代表寻路过程中的期望速度,也就是方向

transform.Translate()函数中,有两相对坐标值,一个是世界坐标,一个是自身坐标,如果第一个坐标不填写的话,默认为自身坐标系

[展开全文]
风旅人 · 2020-02-18 · 该任务已被删除 0

如果 agent type使用的是humanoid,则导航网格的agent type也应该用 

[展开全文]
风旅人 · 2020-02-18 · 该任务已被删除 0

collect object参数改为volume可以只在固定范围内烘焙网格

使用nav mesh surface生成的网格是不考虑nav static的

use geometry可以更改烘焙根据

[展开全文]
风旅人 · 2020-02-18 · 该任务已被删除 0

nav mesh obstacle太多会影响性能,建造类游戏不宜使用

导入官方的插件navmeshcomponent,挂载nav mesh surface即可创建导航网格

使用这种方法的时候,如果要改变导航网格的参数,就必须在agent中添加新的模板,在新

[展开全文]
风旅人 · 2020-02-18 · 该任务已被删除 0

挂载上offmeshlink组件可以自定义跳跃路径的起点和终点

Bi directional意为双向,若取消勾选则为单向

auto update position勾选后可以动态修改起始和结束点

[展开全文]
风旅人 · 2020-02-18 · 该任务已被删除 0

授课教师

SiKi学院老师

课程特色

图文(1)
视频(24)
下载资料(2)