2018年4月19日 星期四

Pass variables to another Batch even if SETLOCAL

File1.bat
@echo off
setlocal Enabledelayedexpansion
set var=value

if %var%==value (
    set var=another value
 echo !var!
)
endlocal & (
  set "var2=%var%"
)
File2.bat
call "File1.bat"
echo %var2%

2015年11月18日 星期三

Unity - Mirror/Flip Mesh

除了將scale x = -1外, 更好的方法如下:

using UnityEngine;

public class flipMesh : MonoBehaviour {

 // Use this for initialization
 void Start () {

        Mesh mesh = GetComponent().mesh;
        Vector2[] uv = mesh.uv;
        int szV = uv.Length;
        Vector2[] newUv = new Vector2[szV];
        for (int j = 0; j < szV; j++)
        {
            newUv[j] = new Vector2(uv[szV - j - 1].x, uv[j].y);
        }
        mesh.uv = newUv;

    }
}

2015年11月12日 星期四

Unity - 攝影機 看到 物體的內部

在需要被看到內部的物體使用此Script

using UnityEngine;
using System.Collections;

public class doubleSideShader : MonoBehaviour {

 // Use this for initialization
 void Start () {
        Mesh mesh = GetComponent().mesh;
        Vector3[] vertices = mesh.vertices;
        Vector2[] uv = mesh.uv;
        Vector3[] normals = mesh.normals;
        int szV = vertices.Length;
        Vector3[] newVerts = new Vector3[szV * 2];
        Vector2[] newUv = new Vector2[szV * 2];
        Vector3[] newNorms = new Vector3[szV * 2];
        for (int j = 0; j < szV; j++)
        {
            // duplicate vertices and uvs:
            newVerts[j] = newVerts[j + szV] = vertices[j];
            newUv[j] = newUv[j + szV] = uv[j];
            // copy the original normals...
            newNorms[j] = normals[j];
            // and revert the new ones
            newNorms[j + szV] = -normals[j];
        }

        int[] triangles = mesh.triangles;
        int szT = triangles.Length;
        int[] newTris = new int[szT * 2]; // double the triangles
        for (int i = 0, j = 0; i < szT; i += 3)
        {
            // copy the original triangle
            newTris[i] = triangles[i];
            newTris[i + 1] = triangles[i + 1];
            newTris[i + 2] = triangles[i + 2];
            // save the new reversed triangle
            j = i + szT;
            newTris[j] = triangles[i] + szV;
            newTris[j + 2] = triangles[i + 1] + szV;
            newTris[j + 1] = triangles[i + 2] + szV;
        }
        mesh.vertices = newVerts;
        mesh.uv = newUv;
        mesh.normals = newNorms;
        mesh.triangles = newTris; // assign triangles last!
    }
 
 // Update is called once per frame
 void Update () {
 
 }
}

2015年11月10日 星期二

Unity - 在inspector中建立按鈕、圖片

using UnityEngine;
using UnityEditor;

[CustomEditor (typeof(customIns))]
class customIns : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        Texture txture = Resources.Load("file_name_without_extension");
        GUILayout.Label(txture);

        if (GUILayout.Button("Button"))
        {
            Debug.Log("It's alive: " + target.name);
        }
            
    }
}

2015年11月6日 星期五

Unity - 在inspector中輸入數值、更改顏色、輸入座標、勾選及下拉式選單

有時在撰寫程式時,希望在遊戲進行中,也能更改參數,


最常見的有
1. 輸入數值,如 int float等
2. 變更顏色
3. 輸入座標或向量
4. 勾選
5. 下拉式選單

效果如下圖


其實要達到非常簡單,
只需將想要在inspector也能更改的數值,設為public即可。
程式碼如下

using UnityEngine;
using System.Collections;

public class inputInGame : MonoBehaviour {

    public int editNum;
    public Color color;
    public Vector3 position;
    public bool check;

    public enum customExpand {choose1, choose2 }
    public customExpand expand;
 
 // Update is called once per frame
 void Update () {
 
 }
}

2015年11月3日 星期二

Unity 紅外線瞄準器

程式碼內附著三種標記方式
大家有興趣可以試試看

使用時,須建立一個空的GameObject,並將此Script拖曳至空的GameObject。
以及把需要當作貼片(模擬成紅外線光圈)的圓形薄片物體拖曳給此Script的Pointer。
貼片請自行設計,可用圓柱物體並將厚度設為0.01即可。



using UnityEngine;
using System.Collections;
using UnityEditor;

public class LaserProject : MonoBehaviour {

    public GameObject pointer;
    private Light spotlight;
    private LineRenderer lineRenderer;

    public enum ProjectTypes {SpotLightTangent, SpotLightNormal, PatchObject};
    public ProjectTypes projectTypes;

    //是否顯示光線
    public bool showLight = true;
    //光源移動離目標距離
    private float minDistance = 1f;
    //定義一個Vector3,用來儲存物件原點位置
    private Vector3 originalPosition;
    //從螢幕射出的光線
    private Ray rayFromCam;
    //沿著螢幕射線慢慢移動的光線
    private Ray ray;

    //a RaycastHit variable, to gather informartion about the ray's collision  
    private RaycastHit hitFromCam, hit;
    
    //用來索引端點
    private int index = 0;


    // Use this for initialization
    void Awake () {

        if (projectTypes != ProjectTypes.PatchObject)
        {
            //取得燈光物件
            spotlight = gameObject.GetComponent();
            spotlight.enabled = false;
            //貼圖設為看不見
            pointer.active = false;

        }

        //紀錄原點位置
        originalPosition = transform.position;

        if (showLight) //顯示光線
        { 
            //添加LineRenderer組件
            lineRenderer = gameObject.AddComponent();
            //設置材質
            lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
            //設置顏色
            lineRenderer.SetColors(Color.white, Color.white);
            //設置寬度
            lineRenderer.SetWidth(0.01f, 0.05f);
            //設置起點與終點,若不設置終點會自動產生線段
            lineRenderer.SetPosition(0, originalPosition);
            lineRenderer.SetPosition(1, originalPosition);
        }

    }
 
 // Update is called once per frame
 void Update () {

        if (Input.GetMouseButton(0)) //點擊左鍵
        {
            if (projectTypes != ProjectTypes.PatchObject)
                spotlight.enabled = false;  //關閉光源


            //從攝像機發出到點擊坐標的射線
            rayFromCam = Camera.main.ScreenPointToRay(Input.mousePosition);
            
            // 如果有射到物體
            if (Physics.Raycast(rayFromCam, out hitFromCam))  
            {
                //取得投影點與對應物體名稱
                //Debug.Log("" + hitFromCam.point.x + ", " + hitFromCam.point.y + ", " + hitFromCam.point.z);
                //GameObject gameObj = hitFromCam.collider.gameObject;
                //Debug.Log("click object name is " + gameObj.name);
                
                Vector3 direction = hitFromCam.point - originalPosition;
                ray = new Ray(originalPosition, direction);
                
                //發射一條100個單位長的射線
                if (Physics.Raycast(ray.origin, ray.direction, out hit, 100))
                {

                    if (showLight)
                    {
                        //獲取LineRenderer組件
                        lineRenderer = GetComponent();
                        lineRenderer.SetPosition(index + 1, hit.point);
                    }

                    //計算距離
                    float distance = Vector3.Distance(originalPosition, hit.point);
                    //Debug.Log("normal  " + hit.normal);

                    switch (projectTypes)
                    {
                        //1. 光源移動到沿射線距離物件minDistance上
                        case ProjectTypes.SpotLightTangent:
                            if (distance > minDistance)
                            {
                                transform.LookAt(hit.point);
                                spotlight.spotAngle = 60;
                                spotlight.range = minDistance * 2;
                                spotlight.transform.position =
                                    Vector3.MoveTowards(originalPosition, hit.point, distance - minDistance);
                                spotlight.enabled = true;
                            }
                            else
                                spotlight.enabled = false;
                            break;

                        //2. 光源移動至法向量上
                        case ProjectTypes.SpotLightNormal:
                            if (distance > minDistance)
                            {
                                transform.LookAt(hit.point);
                                spotlight.spotAngle = 70;
                                spotlight.range = minDistance * 3;
                                spotlight.transform.position = hit.point + hit.normal * minDistance;
                                spotlight.enabled = true;
                            }
                            else
                                spotlight.enabled = false;
                            break;

                        //3.使用半透明物件作為標示物
                        case ProjectTypes.PatchObject:
                            pointer.transform.position = hit.point;
                            pointer.transform.rotation =
                                Quaternion.FromToRotation(pointer.transform.up, hit.normal) * pointer.transform.rotation;
                            break;

                        default:
                            break;

                    }
                    
                }
            }
           
        }

    }
    
}