Monday, 14 January 2019

Unity C# 3D Progress bar - Creating a Progress bar which scales over time

1. Create a progress bar with outer frame(fixed) and a inner slider (scales over time) something as shown in the below pic.
2. Attach the below script to an empty game object and drag the slider game object on to the progress slider



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProgressBar : MonoBehaviour
{
    public GameObject progressSlider;
    float totalWidth;
    float totalDuration = 100.0f;
    float currentTime;
    float currentWidth;


// Use this for initialization
void Start ()
    {
        totalWidth = progressSlider.transform.localScale.x;
        currentWidth = totalWidth / 100;
        progressSlider.transform.localScale = new Vector3(currentWidth, progressSlider.transform.localScale.y, progressSlider.transform.localScale.z);


    }

// Update is called once per frame
void Update ()
    {
        currentTime += Time.deltaTime;
        if(currentTime<totalDuration )
        {
            if(currentWidth <= totalWidth)
            {
                currentWidth += 0.01f;
                progressSlider.transform.localScale = new Vector3(currentWidth, progressSlider.transform.localScale.y, progressSlider.transform.localScale.z);
            }
           
        }


    }
}


That's it run the scene.

Creating a Dictation Recognizer in Unity C# - Hololens

1.Create a new scene and apply the mixed reality scene settings.
2. Create a UI text component and drag on to the inputtext field on the next step after attaching the code.
2. Create a new button and attach the below script and trigger the InitateRecording function on click event


using UnityEditor;

using UnityEngine.UI;
using UnityEngine;
using HoloToolkit.Unity.InputModule;
using UnityEngine.Windows.Speech;

public class DictationHandler : MonoBehaviour {

   
    public GameObject inputtext = null, icon = null;
    public Material recording = null, stopped = null;
 
         [SerializeField]
    private Text m_Hypotheses;

    [SerializeField]
    private Text m_Recognitions;

    private DictationRecognizer m_DictationRecognizer;

    bool isRecording;

    public void InitateRecording()
    {
        if(!isRecording)
        {
            isRecording = true;
            StartRecording();
        }
        else
        {
            isRecording = false;
            StopRecording();
        }
    }

     void StartRecording()
    {
        icon.GetComponent<MeshRenderer>().material = recording;
        m_DictationRecognizer = new DictationRecognizer();

        m_DictationRecognizer.DictationResult += (text, confidence) =>
        {
            Debug.LogFormat("Dictation result: {0}", text);
            inputtext.GetComponent<UnityEngine.UI.Text>().text += text ;
        };

        m_DictationRecognizer.DictationHypothesis += (text) =>
        {
            Debug.LogFormat("Dictation hypothesis: {0}", text);
            m_Hypotheses.text += text;
        };

        m_DictationRecognizer.DictationComplete += (completionCause) =>
        {
            if (completionCause != DictationCompletionCause.Complete)
                Debug.LogErrorFormat("Dictation completed unsuccessfully: {0}.", completionCause);
        };

        m_DictationRecognizer.DictationError += (error, hresult) =>
        {
            Debug.LogErrorFormat("Dictation error: {0}; HResult = {1}.", error, hresult);
        };

        m_DictationRecognizer.Start();
    }
     void StopRecording()
    {
        m_DictationRecognizer.Stop();
        icon.GetComponent<MeshRenderer>().material = stopped;
    }

    }