var x = center.x + radius * Mathf.Cos(rad);
var y = center.y + radius * Mathf.Sin(rad);
四、最後加入物件生成後會像這樣,指定於角度 20 度座標位置生成一個 Ball 物件:
public GameObject Ball;
private void Start()
{
var center = new Vector3(0, 0, 0);
var radius = 3;
var rad = 20 * Mathf.PI / 180;
var x = center.x + radius * Mathf.Cos(rad);
var y = center.y + radius * Mathf.Sin(rad);
Ball.transform.position = new Vector3(x, y, center.z);
Instantiate(Ball);
}
Unity 預覽,紅點為圓心,白點為生成的 Ball。
五、實際運用方面,我們可以用迴圈達成「在圓周上每 20 度生成一個物件」的目的。
public GameObject Ball;
private void Start()
{
var center = new Vector3(0, 0, 0);
var radius = 3;
for (int i = 0; i < 360; i += 20)
{
var rad = i * Mathf.PI / 180;
var x = center.x + radius * Mathf.Cos(rad);
var y = center.y + radius * Mathf.Sin(rad);
Ball.transform.position = new Vector3(x, y, center.z);
Instantiate(Ball);
}
}
假設我們現在有一個空資料表,現在要一次建立多筆產品資料 Product,主鍵為 ID 且不可重複,但這邊假設提供的內容有重複的可能性。
一、建立產品的資料模型
public class Product {
[BsonId]
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; }
public string Name { get; set; }
public float Price { get; set; }
}
二、建立 MongoDB 資料庫連線,並先取好資料庫與集合 Collection。
var connectionString = "mongodb://admin:0000@127.0.0.1:27017";
var client = new MongoClient(connectionString);
var collection = client.GetDatabase("MyStore").GetCollection<Product>("Product");
為了解決我遇到的輸出空白問題,就自行寫了一個單純用 for 迴圈組合字串、沒有做任何合法驗證的方法。這邊有個 params 關鍵字還挺有趣,他可以讓後面接的陣列變成參數化的寫法。
publicclass PathTool
{publicstaticstring Combine(paramsstring[] paths){string result ="";for(int i =0; i < paths.Length; i++){
result += paths[i];if(i +1!= paths.Length){
result +="/";}}return result;}}