void Start() { // ПТ_Мувер основан на ПТ_Лок, который содержит инфу и данных позиции PT_Loc pLoc; List <PT_Loc> locs = new List <PT_Loc>(); // Позиция всегда одинакова и всегда z = -0.1f Vector3 tPos = pos; tPos.z = -0.1f; // У нас должно быть одинаковое число размеров и цветов в инспекторе for (int i = 0; i < scales.Length; i++) { pLoc = new PT_Loc(); pLoc.scale = Vector3.one * scales[i]; // Каждый размер pLoc.pos = tPos; pLoc.color = colors[i]; // и каждый цвет locs.Add(pLoc); } // callback это делегативная функция которая вызывает функцию() когда // движение закончится callback = CallbackMethod; // Вызываем CallbackMethod когда закончим // Создаём движения передавая серию PT_Locs и продолжительность для кривой PT_StartMove(locs, lifeTime); }
private void Start() { PT_Loc pLoc; List <PT_Loc> locs = new List <PT_Loc>(); Vector3 tpos = pos; tpos.z = -0.1f; //在检查器中必须保持相同的比例和颜色 for (int i = 0; i < scales.Length; i++) { pLoc = new PT_Loc(); pLoc.scale = Vector3.one * scales[i]; pLoc.pos = tpos; pLoc.color = colors[i]; locs.Add(pLoc); //回调机制:一种函数授权机制,当移动停止时调用void function()函数 //完成时调用Callbackmethod() callback = CallbackMethod; //通过贝赛尔曲线传递一系列PT_Locs和持续时间来初始化移动 PT_StartMove(locs, lifeTime); } void CallbackMethod() { Destroy(gameObject); //完成移动,销毁gameobject } }
public static PT_Loc Bezier(float u, PT_Loc[] locs) { // Recursively solve this // If there is only 1 PT_Loc in locs, return it if (locs.Length == 1) { return(locs[0]); } int len = locs.Length - 1; // Create locsL, which is all but the last element of locs // e.g. if locs = [0,1,2,3,4] then locsL = [0,1,2,3] PT_Loc[] locsL = new PT_Loc[len]; PT_Loc[].Copy(locs, 0, locsL, 0, len); // Create locsR, which is all but the 0th element of locs // e.g. if locs = [0,1,2,3,4] then locsR = [1,2,3,4] PT_Loc[] locsR = new PT_Loc[len]; PT_Loc[].Copy(locs, 1, locsR, 0, len); // The result is the Lerp of these two shorter Lists PT_Loc res = Lerp(Bezier(u, locsL), Bezier(u, locsR), u); return(res); }
void Start() { // PT_Mover works based on the PT_Loc class, which contains information // about position, rotation, and scale. It's similar to a Transform but // simpler (and Unity won't let us create Transforms at will). PT_Loc pLoc; List<PT_Loc> locs = new List<PT_Loc>(); // The position is always the same and always at z=-0.1f Vector3 tPos = pos; tPos.z = -0.1f; // You must have an equal number of scales and colors in the Inspector for (int i=0; i<scales.Length; i++) { pLoc = new PT_Loc(); pLoc.scale = Vector3.one * scales[i]; // Each scale pLoc.pos = tPos; pLoc.color = colors[i]; // and each color locs.Add(pLoc); // is added to locs } // callback is a function delegate that can call a void function() when // the move is done callback = CallbackMethod; // Call CallbackMethod() when finished // Initiate the move by passing in a series of PT_Locs and duration for // the Bézier curve. PT_StartMove(locs, lifeTime); }
void Start() { //PT_Mover works based on the PT_Loc class, which contains information about position, rotation, and scale. //IT's similar to a Transform but simpler. PT_Loc pLoc; List <PT_Loc> locs = new List <PT_Loc>(); //The position is always the same and always at z = -0.1f Vector3 tPos = pos; tPos.z = -0.1f; //You must have an equal number of scales and colors in the Inspector for (int i = 0; i < scales.Length; i++) { pLoc = new PT_Loc(); pLoc.scale = Vector3.one * scales[i]; //Each scale pLoc.pos = tPos; pLoc.color = colors[i]; //and each color locs.Add(pLoc); //is added to locs } //callback is a function delegate that can call a void function() when the move is done callback = CallbackMethod; //Call CallbackMethod() when finished //Initiate the move by passing in a series of PT_Locs and duration for the Bezier curve PT_StartMove(locs, lifeTime); }
private void Start() { //PT_Mover works based on the PT_Loc class, which contains information //about position , rotation, and scale. It's similar to transform but simpler PT_Loc pLoc; List <PT_Loc> locs = new List <PT_Loc>(); //The position is always the same and always at z=-0.1f; Vector3 tPos = pos; tPos.z = -0.1f; //must have an equal number of scales and colors in the Inspector for (int i = 0; i < scales.Length; i++) { pLoc = new PT_Loc(); pLoc.scale = Vector3.one * scales[i]; pLoc.pos = tPos; pLoc.color = colors[i]; locs.Add(pLoc); } callback = CallbackMethod; // Call CallbackMethod when finished //Initiate the move by passing in a series of PT_Locs and duration for the Bezier curve PT_StartMove(locs, lifeTime); }
// This is the old, less efficient version of the Bezier method /* * public static PT_Loc Bezier(float u, PT_Loc[] locs) { * // Recursively solve this * * // If there is only 1 PT_Loc in locs, return it * if (locs.Length == 1) return(locs[0]); * * int len = locs.Length-1; * // Create locsL, which is all but the last element of locs * // e.g. if locs = [0,1,2,3,4] then locsL = [0,1,2,3] * PT_Loc[] locsL = new PT_Loc[len]; * System.Array.Copy(locs, 0, locsL, 0, len); * // PT_Loc[].Copy(locs, 0, locsL, 0, len); * // Create locsR, which is all but the 0th element of locs * // e.g. if locs = [0,1,2,3,4] then locsR = [1,2,3,4] * PT_Loc[] locsR = new PT_Loc[len]; * System.Array.Copy(locs, 1, locsR, 0, len); * // PT_Loc[].Copy(locs, 1, locsR, 0, len); * * // The result is the Lerp of these two shorter Lists * PT_Loc res = Lerp( Bezier(u, locsL), Bezier(u, locsR), u ); * return( res ); * } */ public static PT_Loc Lerp(PT_Loc l0, PT_Loc l1, float u) { PT_Loc l = new PT_Loc(); l.pos = (1 - u) * l0.pos + u * l1.pos; l.rot = Quaternion.Slerp(l0.rot, l1.rot, u); l.scale = (1 - u) * l0.scale + u * l1.scale; l.color = (1 - u) * l0.color + u * l1.color; return(l); }
public PT_Loc Clone() { PT_Loc l = new PT_Loc(); l.pos = pos; l.rot = rot; l.scale = scale; l.color = color; return(l); }
public void PT_SetLoc(PT_Loc l) { transform.position = l.pos; transform.rotation = l.rot; transform.localScale = l.scale; if (ptm_mats == null) { Renderer[] rends = GetComponentsInChildren<Renderer>(); ptm_mats = new Material[rends.Length]; for (int i=0; i<rends.Length; i++) { ptm_mats[i] = rends[i].material; } } foreach (Material m in ptm_mats) { m.color = l.color; } }
}//end of Awake() void Start(){ PT_Loc pLoc; List<PT_Loc> locs = new List<PT_Loc>(); Vector3 tPos = pos; tPos.z = -0.1f; for (int i = 0; i < scales.Length; i++){ pLoc = new PT_Loc(); pLoc.scale = Vector3.one * scales[i]; pLoc.pos = tPos; pLoc.color = colors[i]; locs.Add(pLoc); }//end of for loop callback = CallbackMethod; PT_StartMove(locs, lifeTime); }//end of Start()
public void PT_SetLoc(PT_Loc l) { transform.position = l.pos; transform.rotation = l.rot; transform.localScale = l.scale; if (ptm_mats == null) { Renderer[] rends = GetComponentsInChildren <Renderer>(); ptm_mats = new Material[rends.Length]; for (int i = 0; i < rends.Length; i++) { ptm_mats[i] = rends[i].material; } } foreach (Material m in ptm_mats) { m.color = l.color; } }
// Update is called once per frame protected virtual void Update() { if (ptm_state == PTM_State.idle || ptm_state == PTM_State.postMove) { return; } // We know that we're set up to move float u = (Time.time - ptm_start) / ptm_duration; float u2 = u; if (u < 0) { ptm_state = PTM_State.preMove; u = 0; PT_SetLoc(ptm_locs[0]); } else if (u > 1) { ptm_state = PTM_State.postMove; u = 1; PT_SetLoc(ptm_locs[ptm_locs.Count - 1]); if (callback != null) { callback(); } } else { ptm_state = PTM_State.move; // Perform easing on u u2 = Easing.Ease(u, ptm_easing); PT_Loc l = PT_Loc.Bezier(u2, ptm_locs); PT_SetLoc(l); } // Place these variables where others can see them ptm_u = u; ptm_u2 = u2; }
// This is the new, fast and efficient version of PT_Loc.Bezier() public static PT_Loc Bezier(float u, PT_Loc[] locs, int iL = 0, int iR = -1) { // iL and iR are indices into the array locs. // In this version, instead of creating lots of new arrays and wasting // memory and time we pass the same array into each recursion and // adjust these indices. if (iR == -1) { iR = locs.Length - 1; } // This is the terminal case when both iL and iR point to the same element if (iL == iR) { return(locs[iL]); } // Since they still point to two different elements of locs, // recur to divide the problem PT_Loc res = PT_Loc.Lerp(Bezier(u, locs, iL, iR - 1), Bezier(u, locs, iL + 1, iR), u); return(res); }
void Start() { PT_Loc pLoc; List <PT_Loc> locs = new List <PT_Loc>(); Vector3 tPos = pos; tPos.z = -0.1f; //The number of scales and colors in the Inspector must be equal for (int i = 0; i < scales.Length; i++) { pLoc = new PT_Loc(); pLoc.scale = Vector3.one * scales[i]; pLoc.pos = tPos; pLoc.color = colors[i]; locs.Add(pLoc); } callback = CallbackMethod; PT_StartMove(locs, lifeTime); }
public static PT_Loc Lerp( PT_Loc l0, PT_Loc l1, float u ) { PT_Loc l = new PT_Loc(); l.pos = (1-u)*l0.pos + u*l1.pos; l.rot = Quaternion.Slerp( l0.rot, l1.rot, u ); l.scale = (1-u)*l0.scale + u*l1.scale; l.color = (1-u)*l0.color + u*l1.color; return( l ); }
public static PT_Loc Bezier(float u, PT_Loc[] locs) { // Recursively solve this // If there is only 1 PT_Loc in locs, return it if (locs.Length == 1) return(locs[0]); int len = locs.Length-1; // Create locsL, which is all but the last element of locs // e.g. if locs = [0,1,2,3,4] then locsL = [0,1,2,3] PT_Loc[] locsL = new PT_Loc[len]; PT_Loc[].Copy(locs, 0, locsL, 0, len); // Create locsR, which is all but the 0th element of locs // e.g. if locs = [0,1,2,3,4] then locsR = [1,2,3,4] PT_Loc[] locsR = new PT_Loc[len]; PT_Loc[].Copy(locs, 1, locsR, 0, len); // The result is the Lerp of these two shorter Lists PT_Loc res = Lerp( Bezier(u, locsL), Bezier(u, locsR), u ); return( res ); }