How turn on method DrawRay into script?

MaxToMax333

New member
Messages
13
Reactions
4
Points
3
Hello! Please tell me, what am I doing wrong?
I do it exactly according to the instructions (DrawRay in Unity) on the network.
There are no errors, the Ray is being created, the RayCast is working, but the Ray is not displayed.

C#:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleJSON;
using UnityEngine.UI;
using UnityEngine.XR;
using UnityEngine.Events;

public class RayTriggerAtomTest : MVRScript {
      
    void Update() {
      
        var vStart = new Vector3(
            transform.position.x + 2.5f, // offset - because when placing an object at 0,0,0, the "transform.position" returns (-2.5, -1.8, 0)
            transform.position.y + 1.8f, // offset - because when placing an object at 0,0,0, the "transform.position" returns (-2.5, -1.8, 0)
            transform.position.z);
          
        var vEnd = transform.forward;
      
        // We launch a beam from the center of the object (with this plugin) in the direction of Z-gizmo
        Ray ray = new Ray(vStart, vEnd);
      
        // Drawing ray from Start in the direction of Z-gizmo and 10 meters long, Green color
        Debug.DrawRay(transform.position, vEnd * 10f, Color.green);
      
        RaycastHit hit;
        if ( Physics.Raycast(ray, out hit) ) {
            SuperController.LogMessage("hit: " + hit.collider.ToString());
        }
    }
}

DrawRay.png
 
Last edited:
Debug lines are only available in the Unity editor.

For drawing lines in the application runtime, you need another solution e.g. one using GL. Here's an example

C#:
using Battlehub.RTCommon;
using System.Collctions.Generic;
using UnityEngine;
using UnityEngine.Rendering;

struct LineV3
{
    public readonly Vector3 from;
    public readonly Vector3 to;
    public readonly Color color;
    public LineV3(Vector3 from, Vector3 to, Color color)
    {
        this.from = from;
        this.to = to;
        this.color = color;
    }
}

class DrawLines : MonoBehaviour, IGL
{
    Material _drawMat;
    public readonly List<LineV3> lines = new List<LineV3>();

    public void Init(bool xray = false)
    {
        var shader = Shader.Find(xray ? "Battlehub/RTHandles/VertexColor" : "Hidden/Internal-Colored");
        var lineMat = new Material(shader)
        {
            hideFlags = HideFlags.HideAndDontSave,
        };
        lineMat.SetInt("_SrcBlend", (int) BlendMode.SrcAlpha);
        lineMat.SetInt("_DstBlend", (int) BlendMode.OneMinusSrcAlpha);
        lineMat.SetInt("_Cull", (int) CullMode.Back);
        lineMat.SetInt("_ZWrite", 0);
        _drawMat = lineMat;
        if(GLRenderer.Instance == null)
        {
            new GameObject().AddComponent<GLRenderer>();
        }

        GLRenderer.Instance.Add(this);
    }
    
    public void Draw(int cullingMask)
    {
        if(!enabled)
        {
            return;
        }

        GL.PushMatrix();
        _drawMat.SetPass(0);
        GL.Begin(GL.LINES);
        for(int i = 0; i < lines.Count; i++)
        {
            var line = lines[i];
            GL.Color(line.color);
            GL.Vertex(line.from);
            GL.Vertex(line.to);
        }

        GL.End();
        GL.PopMatrix();
    }
    
    void OnDisable() => lines.Clear();

    void OnDestroy()
    {
        if(GLRenderer.Instance != null)
        {
            GLRenderer.Instance.Remove(this);
        }

        UnityEngine.Object.Destroy(_drawMat);
    }
}

// in your MVRScript

var drawLines = gameObject.AddComponent<DrawLines>();
drawLines.Init();

void Update()
{
    drawLines.lines.Clear();
    drawLines.lines.Add(new LineV3(Vector3.zero, Vector3.one, Color.white);
}
 

everlaster, could you tell me which literature to read in order to understand what is available in VAM-script and what is not available. Is there a description of objects, types, methods and other things?
As I understand it, the Unity instructions are not really suitable for developing plugins for VAM, right?​

 
@MaxToMax333

"which literature to read":
1. There is no official documentation
2. Decompile the vam dll. It is located in VaM_Data/Managed/Assembly-CSharp.dll. Download a c# decompiler application or an editor that supports decompiling while editing (Rider is great but paid). The decompiled dll is fair game for learning how things work and building plugins based off of that.
3. Set up your project with a .csproj file that references the .dll's in VaM_Data/Managed and ensure your editor supports C#/.NET development (Intellisense) so that you can navigate to methods, get suggestions for method names see method signatures etc. E.g. https://github.com/acidbubbles/vam-plugin-template
4. Plugins created by other people are good learning material. If you copy-paste implementations from other plugins, be sure to check the license allows that and then provide credit as needed.

"As I understand it, the Unity instructions are not really suitable for developing plugins for VAM, right?"

Sure they are. VAM is built on Unity so basically anything that's available in Unity 2018.1 works. However as VAM is a layer on top of Unity, there's a lot of VAM specific logic that you need to keep in mind.
 
@MaxToMax333

"which literature to read":
1. There is no official documentation
2. Decompile the vam dll. It is located in VaM_Data/Managed/Assembly-CSharp.dll. Download a c# decompiler application or an editor that supports decompiling while editing (Rider is great but paid). The decompiled dll is fair game for learning how things work and building plugins based off of that.
3. Set up your project with a .csproj file that references the .dll's in VaM_Data/Managed and ensure your editor supports C#/.NET development (Intellisense) so that you can navigate to methods, get suggestions for method names see method signatures etc. E.g. https://github.com/acidbubbles/vam-plugin-template
4. Plugins created by other people are good learning material. If you copy-paste implementations from other plugins, be sure to check the license allows that and then provide credit as needed.

"As I understand it, the Unity instructions are not really suitable for developing plugins for VAM, right?"

Sure they are. VAM is built on Unity so basically anything that's available in Unity 2018.1 works. However as VAM is a layer on top of Unity, there's a lot of VAM specific logic that you need to keep in mind.
Thank you very much!
 
Debug lines are only available in the Unity editor.

For drawing lines in the application runtime, you need another solution e.g. one using GL. Here's an example

C#:
using Battlehub.RTCommon;
using System.Collctions.Generic;
using UnityEngine;
using UnityEngine.Rendering;

struct LineV3
{
    public readonly Vector3 from;
    public readonly Vector3 to;
    public readonly Color color;
    public LineV3(Vector3 from, Vector3 to, Color color)
    {
        this.from = from;
        this.to = to;
        this.color = color;
    }
}

class DrawLines : MonoBehaviour, IGL
{
    Material _drawMat;
    public readonly List<LineV3> lines = new List<LineV3>();

    public void Init(bool xray = false)
    {
        var shader = Shader.Find(xray ? "Battlehub/RTHandles/VertexColor" : "Hidden/Internal-Colored");
        var lineMat = new Material(shader)
        {
            hideFlags = HideFlags.HideAndDontSave,
        };
        lineMat.SetInt("_SrcBlend", (int) BlendMode.SrcAlpha);
        lineMat.SetInt("_DstBlend", (int) BlendMode.OneMinusSrcAlpha);
        lineMat.SetInt("_Cull", (int) CullMode.Back);
        lineMat.SetInt("_ZWrite", 0);
        _drawMat = lineMat;
        if(GLRenderer.Instance == null)
        {
            new GameObject().AddComponent<GLRenderer>();
        }

        GLRenderer.Instance.Add(this);
    }
   
    public void Draw(int cullingMask)
    {
        if(!enabled)
        {
            return;
        }

        GL.PushMatrix();
        _drawMat.SetPass(0);
        GL.Begin(GL.LINES);
        for(int i = 0; i < lines.Count; i++)
        {
            var line = lines[i];
            GL.Color(line.color);
            GL.Vertex(line.from);
            GL.Vertex(line.to);
        }

        GL.End();
        GL.PopMatrix();
    }
   
    void OnDisable() => lines.Clear();

    void OnDestroy()
    {
        if(GLRenderer.Instance != null)
        {
            GLRenderer.Instance.Remove(this);
        }

        UnityEngine.Object.Destroy(_drawMat);
    }
}

// in your MVRScript

var drawLines = gameObject.AddComponent<DrawLines>();
drawLines.Init();

void Update()
{
    drawLines.lines.Clear();
    drawLines.lines.Add(new LineV3(Vector3.zero, Vector3.one, Color.white);
}
I think, I did wrong: I inserted your entire example into my plugin-script, and moved the "in your MVRScript" block to block "MVRScript". But it doesn't work, the system swears at "Unexpected symbol `(' in class, struct, or interface member declaration in .../RayTriggerAtomTest.cs at [83, 16]"
 

Attachments

  • DrawLine.png
    DrawLine.png
    23 KB · Views: 0
You need to develop in an editor with C# support to see syntax errors highlighted in-editor without having to have VAM compile the plugin
Is the general structure of the script correct?
This is definitely necessary. I'm just learning how to set up VS Code / Visual Studio with Unity.
Thanks a lot for the tips. I will study it)
 
This is definitely necessary. I'm just learning how to set up VS Code / Visual Studio with Unity.
Since VAM plugins aren't developed with Unity, but as "standalone" C# projects (depending only on the dll's that exist in the Managed folder) that are then compiled dynamically in the VAM runtime rather than in the Unity editor, you don't need Unity support in VSCode. You just need C# support.

EDIT: and of course the correct targeting pack / SDK. The unity version VAM is running uses a version of Mono that's roughly equivalent to .NET 3.5. IIRC the way I installed it was just through Windows Features
1716756777773.png


If that doesn't do it, try the Visual Studio route https://stackoverflow.com/questions...net-framework-3-5-sp1-into-visual-studio-2017
 
Last edited:
Debug lines are only available in the Unity editor.

For drawing lines in the application runtime, you need another solution e.g. one using GL. Here's an example

C#:
using Battlehub.RTCommon;
using System.Collctions.Generic;
using UnityEngine;
using UnityEngine.Rendering;

struct LineV3
{
    public readonly Vector3 from;
    public readonly Vector3 to;
    public readonly Color color;
    public LineV3(Vector3 from, Vector3 to, Color color)
    {
        this.from = from;
        this.to = to;
        this.color = color;
    }
}

class DrawLines : MonoBehaviour, IGL
{
    Material _drawMat;
    public readonly List<LineV3> lines = new List<LineV3>();

    public void Init(bool xray = false)
    {
        var shader = Shader.Find(xray ? "Battlehub/RTHandles/VertexColor" : "Hidden/Internal-Colored");
        var lineMat = new Material(shader)
        {
            hideFlags = HideFlags.HideAndDontSave,
        };
        lineMat.SetInt("_SrcBlend", (int) BlendMode.SrcAlpha);
        lineMat.SetInt("_DstBlend", (int) BlendMode.OneMinusSrcAlpha);
        lineMat.SetInt("_Cull", (int) CullMode.Back);
        lineMat.SetInt("_ZWrite", 0);
        _drawMat = lineMat;
        if(GLRenderer.Instance == null)
        {
            new GameObject().AddComponent<GLRenderer>();
        }

        GLRenderer.Instance.Add(this);
    }
   
    public void Draw(int cullingMask)
    {
        if(!enabled)
        {
            return;
        }

        GL.PushMatrix();
        _drawMat.SetPass(0);
        GL.Begin(GL.LINES);
        for(int i = 0; i < lines.Count; i++)
        {
            var line = lines[i];
            GL.Color(line.color);
            GL.Vertex(line.from);
            GL.Vertex(line.to);
        }

        GL.End();
        GL.PopMatrix();
    }
   
    void OnDisable() => lines.Clear();

    void OnDestroy()
    {
        if(GLRenderer.Instance != null)
        {
            GLRenderer.Instance.Remove(this);
        }

        UnityEngine.Object.Destroy(_drawMat);
    }
}

// in your MVRScript

var drawLines = gameObject.AddComponent<DrawLines>();
drawLines.Init();

void Update()
{
    drawLines.lines.Clear();
    drawLines.lines.Add(new LineV3(Vector3.zero, Vector3.one, Color.white);
}
There were two typos:
1. using System.CollEctions.Generic;
2. drawLines.lines.Add(new LineV3(Vector3.zero, Vector3.one, Color.white));

But now it's working!))))))
THANK YOU!!!!!!
It remains only to figure out the coordinates when moving and rotating the object. But that's another story.

C#:
using Battlehub.RTCommon;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class RayTriggerAtomTest : MVRScript {
    
    private DrawLines drawLines;
    
    struct LineV3
    {
        public readonly Vector3 from;
        public readonly Vector3 to;
        public readonly Color color;
        
        public LineV3(Vector3 from, Vector3 to, Color color)
        {
            this.from = from;
            this.to = to;
            this.color = color;
        }
    }

    class DrawLines : MonoBehaviour, IGL
    {
        Material _drawMat;
        public readonly List<LineV3> lines = new List<LineV3>();

        public void Init(bool xray = false)
        {
            var shader = Shader.Find(xray ? "Battlehub/RTHandles/VertexColor" : "Hidden/Internal-Colored");
            var lineMat = new Material(shader)
            {
                hideFlags = HideFlags.HideAndDontSave,
            };
            lineMat.SetInt("_SrcBlend", (int) BlendMode.SrcAlpha);
            lineMat.SetInt("_DstBlend", (int) BlendMode.OneMinusSrcAlpha);
            lineMat.SetInt("_Cull", (int) CullMode.Back);
            lineMat.SetInt("_ZWrite", 0);
            _drawMat = lineMat;
            if(GLRenderer.Instance == null)
            {
                new GameObject().AddComponent<GLRenderer>();
            }

            GLRenderer.Instance.Add(this);
        }
        
        public void Draw(int cullingMask)
        {
            if(!enabled)
            {
                return;
            }

            GL.PushMatrix();
            _drawMat.SetPass(0);
            GL.Begin(GL.LINES);
            for(int i = 0; i < lines.Count; i++)
            {
                var line = lines[i];
                GL.Color(line.color);
                GL.Vertex(line.from);
                GL.Vertex(line.to);
            }

            GL.End();
            GL.PopMatrix();
        }
        
        void OnDisable() => lines.Clear();

        void OnDestroy()
        {
            if(GLRenderer.Instance != null)
            {
                GLRenderer.Instance.Remove(this);
            }

            UnityEngine.Object.Destroy(_drawMat);
        }
    }

    public override void Init()
    {
        drawLines = gameObject.AddComponent<DrawLines>();
        drawLines.Init();
    }
    
    void Update()
    {
        drawLines.lines.Clear();
        //drawLines.lines.Add(new LineV3(Vector3.zero, Vector3.one, Color.white));
        
        var vStart = new Vector3(
            transform.position.x + 2.5f,
            transform.position.y + 1.8f,
            transform.position.z);
            
        var vEnd = transform.forward;
        
        drawLines.lines.Add(new LineV3(vStart, vEnd, Color.green));
    }
}

Ray_example.png
Ray_normal.png
Ray_PositionY-1.png

[
Ray_RotationY+90.png
/SPOILER]
 
Back
Top Bottom