baobao
1/7/2018 - 3:33 PM

Convert.cs

using UnityEngine;

public class Convert : MonoBehaviour
{
    void Start ()
    {
        Hoge t = new Hoge (5);
        // Hoge型をint型に変換
        int a = t;
        Debug.Log (a);

        // int型からHogeを生成
        Hoge t2 = 10;
        Debug.Log (t2);
    }
}

public struct Hoge
{
    int m_num;

    public Hoge (int num)
    {
        this.m_num = num;
    }
    // Hoge -> int
    public static implicit operator int (Hoge value)
    {
        return value.m_num;
    }
    // int -> Hoge
    public static implicit operator Hoge (int value)
    {
        return new Hoge (value);
    }
}