2017年7月28日 星期五

Unity 基本碰撞範例


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

[RequireComponent(typeof(Rigidbody))]
public class Bullet : MonoBehaviour {

    float speed = 100;

    void Update()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * speed);
        Destroy(gameObject, 3);
    }

    void OnCollisionEnter(Collision collider)
    {
        if (collider.gameObject.name == "物件名稱")
            Debug.Log("aa");
    }
}


2017年7月16日 星期日

更改螢幕大小(解析度)

因由之前製作遊戲過程當中,想要讓玩家調整螢幕之解析度,因此測試了這項的小功能。
意外發現到與Unity本身發佈出來後之解析度有些不同 (目前小弟還在進行研究當中)。

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

public class ScreenResolution : MonoBehaviour
{
    // resolutoion X, Y
    public List resolutionX = new List();
    public List resolutionY = new List();

    public int status;

    void Start()
    {
        resolutionFiltering();
    }

    void OnGUI()
    {
        GUILayout.BeginArea(new Rect(Screen.width / Screen.width, Screen.height / Screen.height, 200, Screen.height));

        if (GUILayout.Button("螢幕解析度切換+"))
            status += 1;

        if (status >= resolutionX.Count && status >= resolutionY.Count)
            status = 0;

        if (GUILayout.Button("確定"))
            Screen.SetResolution(resolutionX[status], resolutionY[status], true);
    

        GUILayout.Label(resolutionX[status].ToString() + " + " + resolutionY[status].ToString());
        GUILayout.Label("目前解析度:" + Screen.width + " + " + Screen.height);
        GUILayout.EndArea();

        Screen.fullScreen = false;  // 不全屏: false  全屏: true
    }

    /// 
    /// 螢幕解析度
    /// 過濾重複的數值
    /// 
    void resolutionFiltering()
    {
        Resolution[] resolutions = Screen.resolutions;

        foreach (Resolution res in resolutions)
        {
            resolutionX.Add(res.width);
            resolutionY.Add(res.height);
        }

        for (int i = resolutionX.Count - 1; i > 0; i--)
        {
            if (resolutionX.IndexOf(resolutionX[i]) != i)
                resolutionX.RemoveAt(i);
        }

        for (int i = resolutionY.Count - 1; i > 0; i--)
        {
            if (resolutionY.IndexOf(resolutionY[i]) != i)
                resolutionY.RemoveAt(i);
        }
    }
}

------------
結果圖:




2017年7月9日 星期日

Sprite to Texture2D

Sprite 轉 Texture2D 程式:

    /// 
    /// sprite to Textrue2D
    /// 
    /// 
    /// 
    Texture2D textureFromSprite(Sprite sprite)
    {
        if (sprite.rect.width != sprite.texture.width)
        {
            Texture2D image = new Texture2D(((int)sprite.rect.width), (int)sprite.rect.height);
            Color[] imageColors = sprite.texture.GetPixels((int)sprite.textureRect.x,
                                                           (int)sprite.textureRect.y,
                                                           (int)sprite.textureRect.width,
                                                           (int)sprite.textureRect.height);
            image.SetPixels(imageColors);
            image.Apply();

            return image;
        } else
            return sprite.texture;
    }