2014年5月14日 星期三

[C#] money string 數字轉成金錢的字串 (插入逗號)

因為專案需要,所以要把數字每三位數插入一個逗號,
之前再寫as3時還傻傻自己寫了一個程式來轉換
這次寫C#遇到同樣的問題
本來打算也寫一隻轉換程式
後來才發現C#內建好了
參考微軟官網

程式的範例

uint playerCoin = 2369000;

playerCoin.ToString("N0");

輸出結果 => 2,369,000

稍微說明一下 ToString("N0")

裡面的N表示輸出的格式為 Number

其他的格式選擇請參考微軟官網

後面的0表示要到小數點第幾位

因為我不需要小數點以後的數字
所以就打0

剩下的就自行參考官網囉^^


2014年5月13日 星期二

[Unity3D] 遊戲暫停時 動畫與音樂的處理

節錄官方範例的script裡的method

private List<Animation> animationComponents;
private List<AudioSource> audioSourceComponents;

void HandleGamePauseRtGame(){ _GamePausePanel.SetActive(false); StartCoroutine(Pause (false)); } void HandleGamePauseOnPress(){ _GamePausePanel.SetActive(true); //_GamePausePanel.transform.position = new Vector3(0,0,0); StartCoroutine(Pause (true)); }

IEnumerator Pause (bool pause) {
// Pause/unpause time
Time.timeScale = (pause ? 0 : 1);
// Unlock/Lock cursor
Screen.lockCursor = !pause;

if (pause == true) {
Object[] objects = FindObjectsOfType(typeof(Animation));
animationComponents = new List<Animation>();
foreach (Object obj in objects) {
Animation anim = (Animation)obj;
if (anim != null && anim.enabled) {
animationComponents.Add(anim);
anim.enabled = false;
}
}
objects = FindObjectsOfType(typeof(AudioSource));
audioSourceComponents = new List<AudioSource>();
foreach (Object obj in objects) {
AudioSource source = (AudioSource)obj;
if (source != null && source.enabled /*&& source.isPlaying*/) {
audioSourceComponents.Add(source);
source.Pause();
}
}
}
else {
// If unpausing, wait one frame before we enable animation component.
// Procedural adjustments are one frame delayed because first frame
// after being paused has deltaTime of 0.
yield return 0;
foreach (Animation anim in animationComponents)
anim.enabled = true;
foreach (AudioSource source in audioSourceComponents)
source.Play();
animationComponents = null;
}
}


2014年4月27日 星期日

2014年4月24日 星期四

[Unity3D] animation material 動畫材質的作法

我這邊寫了一個class,還不夠完美,先湊合著用吧^^
=========================================================
using UnityEngine;
using System.Collections;

public class AnimPlane : MonoBehaviour {

public Texture[] frames;
public bool loop = false;
public float delayTime = 0.01f;
public bool bAlwaysFaceToUser = false;

Quaternion offset=Quaternion.Euler(-90, 0, 0);
Transform  cam;
bool       bEnableAnim = false;

void Start () {

}

        // Use this for initialization
protected void Init(){
gameObject.SetActive(false);

cam=Camera.main.transform;
}

// Update is called once per frame
void Update () {
if(bAlwaysFaceToUser){
Quaternion rot=cam.rotation*offset;

transform.rotation=rot;
Vector3 dirRight=transform.TransformDirection(-Vector3.right);
}
}

public void PlayAnim()
{
gameObject.SetActive(true);
bEnableAnim = true;
StartCoroutine(IEPlayAnim());
}

public void StopAnim()
{
bEnableAnim = false;
StopCoroutine("IEPlayAnim");
gameObject.SetActive(false);
}

IEnumerator IEPlayAnim()
{
int index = 0;
while(bEnableAnim)
{
renderer.material.mainTexture = frames[index];
index++;
if(index >= frames.Length) {
if(loop) index = 0;
else{
gameObject.SetActive(false);
break;
}
}
//yield return null;
yield return new WaitForSeconds(delayTime);
}
}

}
==========================================================
然後寫一個新的script繼承它

using UnityEngine;
using System.Collections;

public class RunnerSpray :AnimPlane {

// Use this for initialization
void Start () {
base.Init();

PlayAnim();
}

}

=========================================================
然後在unity editor裡建立一個plane
將RunnerSpray腳本拖ㄅ放到plane裡

再將多張texture依序拖放到Frames 底下
(這裡有個缺點,萬一圖片有幾百張不就拖到死了,暫時沒空去處理)

然後拖一張texture到這平面上
shader設定成 Mobile/Particles/Additive 

設定大概參考圖片



然後執行一下Run
就能看到效果囉^^








[Unity3D] 關於character controller 的 isGrounded 的問題

原本是用
character.isGrounded 來判斷
但是自己使用的感想覺得不是非常準確
常常偵測不到
(當然可能是某個條件未滿足所造成的)

參考了這兩篇文章後

http://answers.unity3d.com/questions/424047/character-controller-isgroundedfalse-while-moving.html

http://answers.unity3d.com/questions/196381/how-do-i-check-if-my-rigidbody-player-is-grounded.html

整理出以下的方法
用Raycast去偵測
確實能work
參考看看^^

private float distToGround;

bool IsGrounded() {
//return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 1.1f);
}

原本網友是用0.1f 但是我跑了以後偵測不到
可能跟每個人的collider大小有關
我調到1.1f後就能正常偵測了^^




2014年4月22日 星期二

[Unity3D] [單位] 取得gameobject的單位大小

有時候你會想知道這個3d 物件在unity裡到底是佔有多大的單位(unit)
可以用此語法

gameObject.renderer.bounds.size  (回傳值type是  vector3 )

[Unity3D] [Sync MonoDevelop project 的作用] 解決 4.3.4版unity monodevelop 程式的autocomplete功能失效

自從將unity3d從4.2.1升級到4.3.4後
我發現我的monodevelop裡的程式的autocomplete的功能都失效了
這樣往後寫程式都很麻煩,要自己去找reference,
主要是因為這版的unity產生的project,
都沒有自動產生monodevelop相對應的sln文件,
因此造成用mono打開腳本後,
無法使用autocomplete的功能。

後來找到解決的方法
就是在unity editor裡 -> Assets -> Sync MonoDevelop Project





















這樣回到你的unity project資料夾底下一看
就會發現相關的sln文件自動產生了
如此一來autocomplete功能也能正常使用囉^^