Tuesday, 21 May 2019

Unity C# Sorting and Displaying Mesh Renderers in layers

1. Create a new C# script as "SetSortingLayer" and paste the below contents.
2. Attach the script to game objects with Mesh Renderers set the sorting layer. 0 being the lowest of the base layer and the top layer goes 0n increment



using UnityEngine;

 [ExecuteInEditMode]
 public class SetSortingLayer : MonoBehaviour {
     public Renderer MyRenderer;
     public string MySortingLayer;
     public int MySortingOrderInLayer;
   
     // Use this for initialization
     void Start () {
         if (MyRenderer == null) {
             MyRenderer = this.GetComponent<Renderer>();
         }
           

         SetLayer();
     }


     public void SetLayer() {
         if (MyRenderer == null) {
             MyRenderer = this.GetComponent<Renderer>();
         }
           
         MyRenderer.sortingLayerName = MySortingLayer;
         MyRenderer.sortingOrder = MySortingOrderInLayer;
       
         //Debug.Log(MyRenderer.sortingLayerName + " " + MyRenderer.sortingOrder);
     }
 
 }

Monday, 20 May 2019

Unity C# - Hololens Creating an intractable model on Run time

To attach a script to a game object as component during run time, create a new C# script  and paste the below contents.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HoloToolkit.Unity.InputModule.Utilities.Interactions;

public class AttachScript : MonoBehaviour
{
    public GameObject  DmuModel;

    void Start()
    {
         Transform[] allchild = DmuModel.GetComponentsInChildren<Transform>();
           foreach (Transform child in allchild)
             {

              child.gameObject.AddComponent<MeshCollider>();
               child.gameObject.AddComponent<TwoHandManipulatable>();
           }

    }

}

Monday, 13 May 2019

3D Text Mesh Wrapping in Unity using C#

To use the text mesh wrapper snippet create a new 3D Text component in unity and then create a new C# script and name it as "WrapText" and replace the content with the below code snippet.

Then attach the code to the 3D text mesh  and under the 'Text Mesh" field in Wrap text component drag the same 3D text mesh.

Under the Max Line Chars field you can set the length of a particular sentence.

That's  it.

-------CODE SNIPPET-------------------COPY THE BELOW CONTENT-------------------------

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class WrapText : MonoBehaviour
{
    public TextMesh textMesh;

    public int maxLineChars = 35;
    private int lastMaxLineChars = 35;

    string wrappedText = "";
    public bool disableWhilePlaying = true;

    void Awake()
    {
       // bool shouldDisable = disableWhilePlaying  !(Application.isEditor  !Application.isPlaying);
       // this.enabled = !shouldDisable;
    }

    void Update()
    {
        if (textMesh.text != wrappedText || maxLineChars != lastMaxLineChars)
        {
            int charCount = 0;
            wrappedText = "";
            string line = "";

            char[] separators = new char[] { ' ', '\n', '\t' };
            string[] words = textMesh.text.Split(separators);

            for (int i = 0; i < words.Length; i++)
            {
                string word = words[i].Trim();

                if (i == 0)
                {
                    line = word;
                    charCount = word.Length;
                }
                else
                {
                    if ((charCount + (charCount > 0 ? 1 : 0) + word.Length) <= maxLineChars)
                    {
                        if (charCount > 0)
                        {
                            line += ' ';
                            charCount += 1;
                        }

                        line += word;
                        charCount += word.Length;
                    }
                    else
                    {
                        if (wrappedText.Length > 0)
                            wrappedText += '\n';

                        wrappedText += line;

                        line = word;
                        charCount = word.Length;
                    }
                }
            }

            if (charCount > 0)
            {
                if (wrappedText.Length > 0)
                    wrappedText += '\n';

                wrappedText += line;
            }

            textMesh.text = wrappedText;
            lastMaxLineChars = maxLineChars;
        }
    }
}