using System.Collections.Generic; using System.Linq; using Unity.VisualScripting; using UnityEngine; public class UnifyWalls : MonoBehaviour { static GameObject WallContainer; static List[] NewListRecipe; public static void FollowOnSameDirection() { Get.o2("Canvas","Panel_DRAWPlan_NEW").SetActive(true); WallContainer=Get.o2("Plan2D_UI","WallLineContainer"); List ListRecipe = new (){}; foreach (Transform child in WallContainer.transform) { ListRecipe.Add(child); } int Qty = WallContainer.transform.childCount; NewListRecipe = new List[Qty]; _G.ExteriorWallPoints = new List[Qty]; int IndNR=0; for (int i=0; i< Qty; i++ ) { NewListRecipe[i]=new(){}; int Next=i+1; int Prev=i-1; if(Next>Qty-1)Next=0; if(Prev<0)Prev=Qty-1; bool isNext=true; bool isPrev=true; Transform L = WallContainer.transform.GetChild(i); if(ListRecipe.Contains(L)) { Transform N = WallContainer.transform.GetChild(Next); Transform P = WallContainer.transform.GetChild(Prev); //print("L.name===="+L.name); NewListRecipe[IndNR].Add(L); ListRecipe.Remove(L); while(isNext==true) { if(MatchPointFollow(i, Next) && ListRecipe.Contains(N)){ //print("MatchPointFollow===Next="+Next); NewListRecipe[IndNR].Add(N); ListRecipe.Remove(N);} else {isNext=false;} } while(isPrev==true) { if(MatchPointFollow(i, Prev) && ListRecipe.Contains(P)){ //print("MatchPointFollow===Prev="+Prev); NewListRecipe[IndNR].Add(P); ListRecipe.Remove(P);} else {isPrev=false;} } IndNR+=1; } Get.o2("Canvas","Panel_DRAWPlan_NEW").SetActive(false); } // for(int i=0; i < NewListRecipe.Length; i++) // { // foreach(Transform L in NewListRecipe[i]){ // print(i.ToString()+" Transform name==="+L.name); // } // } RecreateWallRecipe(); } public static void RecreateWallRecipe(){ _G.NWExterior=0; for(int i=0; i Listvectors=new(){}; Vector3 NewPoint; foreach(Transform Line in NewListRecipe[i]){ //print(i+" Line to Get Points ON==="+Line.name); //Point start //if(i==2)print(i+" Line.name=="+Line.name); Vector2 PointStart=Line.GetComponent()._pointStart.GetComponent().GetWorldPosition(); NewPoint=new(PointStart.x,-_G.HEIGHT*0.5f,PointStart.y); if(!Listvectors.Contains(NewPoint)){ Listvectors.Add(NewPoint); //print(i.ToString()+ " Add point Start floor"+NewPoint); } //Point End Vector2 PointEnd=Line.GetComponent()._pointEnd.GetComponent().GetWorldPosition(); NewPoint=new(PointEnd.x,-_G.HEIGHT*0.5f,PointEnd.y); if(!Listvectors.Contains(NewPoint)){ Listvectors.Add(NewPoint); //print(i.ToString()+ " Add point End floor"+NewPoint); } //Point Height start float PointStartHeight=Line.GetComponent()._pointStart.GetComponent()._PointHeight; NewPoint=new(PointStart.x,-_G.HEIGHT*0.5f+PointStartHeight,PointStart.y); if(!Listvectors.Contains(NewPoint)){ Listvectors.Add(NewPoint); //print(i.ToString()+ " Add point Start height"+NewPoint); } //Point Height End float PointEndHeight=Line.GetComponent()._pointEnd.GetComponent()._PointHeight; NewPoint=new(PointEnd.x,-_G.HEIGHT*0.5f+PointEndHeight,PointEnd.y); if(!Listvectors.Contains(NewPoint)){ Listvectors.Add(NewPoint); //print(i.ToString()+ " Add point End height"+NewPoint); } _G.WallsAngle[i]=GetWallAngleByDirection(PointStart,PointEnd); } List ListVectorOffset =new(){}; foreach(Vector3 v in Listvectors){ Vector3 Poffset=v+_G.RoomOffset; if(!ListVectorOffset.Contains(Poffset))ListVectorOffset.Add(Poffset); //print("Point from Unify==="+Poffset); } _G.ExteriorWallPoints[i]=ListVectorOffset; Vector3 CenterPoint=Create3D.GetCentroid(_G.ExteriorWallPoints[i]); _G.WallsPointCenter[i]=new(CenterPoint.x,CenterPoint.z); //print("Point from Unify=== Wall number ======================================="+(i+1).ToString()); foreach(Vector3 v in ListVectorOffset){ } } } public static bool MatchPointFollow(int i, int o){ Transform Line = WallContainer.transform.GetChild(i); Transform other = WallContainer.transform.GetChild(o); Vector2 LineStart=Line.GetComponent()._pointStart.GetAnchoredPosition(); Vector2 LineEnd=Line.GetComponent()._pointEnd.GetAnchoredPosition(); Vector2 otherStart=other.GetComponent()._pointStart.GetAnchoredPosition(); Vector2 otherEnd=other.GetComponent()._pointEnd.GetAnchoredPosition(); Vector2 DirectionLine = LineEnd-LineStart; Vector2 Directionother = otherEnd-otherStart; bool isDirection=false; if( DirectionLine.normalized == Directionother.normalized ){isDirection=true;} bool isPointOverLap=false; if( LineStart == otherEnd || otherStart == LineEnd ){isPointOverLap=true;} if(isDirection && isPointOverLap)return true; return false; } public static void CreateFloorPoints(){ _G.FloorPoints = new List[1]; _G.FloorPoints[0]=new(){}; for(int i=0; i<_G.NWExterior; i++){ foreach(Vector3 V in _G.ExteriorWallPoints[i]) { if(V.y==-_G.HEIGHT*0.5f) { Vector3 NewPoint=new(V.x,0,V.z); if(!_G.FloorPoints[0].Contains(NewPoint)) { _G.FloorPoints[0].Add(NewPoint); } } } } } public static float GetWallAngleByDirection(Vector2 Point1,Vector2 Point2){ Vector2 direction=Point2-Point1; direction=direction.normalized; Vector2 xAxis = Vector2.right; // (1, 0) float angle = Vector2.Angle(direction, xAxis); float cross = direction.x * xAxis.y - direction.y * xAxis.x; if (cross < 0) // Negative cross means clockwise { angle = 360f - angle; } //print("Angle from unify===="+angle); return angle; } }