public FishPath loadOnePath(string filepath) { FishPath onePath = ScriptableObject.CreateInstance <FishPath>(); if (filepath == null || filepath.Length == 0) { Debug.Log("路径文件名不正确"); return(onePath); } if (File.Exists(filepath) == false) { Debug.Log("路径文件不存在"); return(onePath); } FileStream fs = new FileStream(filepath, FileMode.Open); StreamReader sr = new StreamReader(fs); while (sr.Peek() >= 0) { string oneline = sr.ReadLine(); string[] cells = oneline.Split('\t'); FishPathControlPoint keyPoint = ScriptableObject.CreateInstance <FishPathControlPoint>(); keyPoint.mRotationChange = float.Parse(cells[1]); keyPoint.mSpeedScale = float.Parse(cells[2]); keyPoint.mTime = 0.5f; onePath.AddPoint(keyPoint); } return(onePath); }
public void initialize(string filePath) { string[] allines = File.ReadAllLines(filePath); FishPath currentPath = null; for (int i = 0; i < allines.Length; i++) { if (allines[i].Length <= 1) { continue; } if (allines[i][0] == '#') { continue; } if (allines[i][0] == '0') //新的路径 { currentPath = ScriptableObject.CreateInstance <FishPath>(); pathList.Add(currentPath); } string[] cells = allines[i].Split('\t'); FishPathControlPoint keyPoint = ScriptableObject.CreateInstance <FishPathControlPoint>(); //keyPoint.id = int.Parse(cells[0]); keyPoint.mRotationChange = float.Parse(cells[1]); keyPoint.mSpeedScale = float.Parse(cells[2]); keyPoint.rotateFactor = float.Parse(cells[3]); keyPoint.speedFactor = float.Parse(cells[4]); keyPoint.mTime = 0.5f; currentPath.AddPoint(keyPoint); } }
private void LoadAllPathes() { for (int i = 0; i < 6; i++) { TextAsset ta = Resources.Load <TextAsset>("Pathes/" + i.ToString()); //Debug.Log(ta == null); if (ta != null) { string jsonStr = ta.text; if (jsonStr != null && jsonStr.Length > 0) { JsonPath jsonPath = new JsonPath(); jsonPath = JsonMapper.ToObject <JsonPath>(jsonStr); if (jsonPath == null) { continue; } FishPath fishPath = ScriptableObject.CreateInstance <FishPath>(); fishPath.lineColour = new Color(jsonPath.r, jsonPath.g, jsonPath.b); fishPath.baseSpeed = (float)jsonPath.baseSpeed; foreach (JsonControlPoint point in jsonPath.pointList) { FishPathControlPoint fpcp = ScriptableObject.CreateInstance <FishPathControlPoint>(); fpcp.mSpeedScale = (float)point.speedScale; fpcp.mRotationChange = new Vector2((float)point.rx, (float)point.ry); fpcp.mTime = (float)point.time; fishPath.AddPoint(fpcp); } mFishPathMap.Add(i, fishPath); } } } }
public void AddPoint(FishPathControlPoint fpcp) { List <FishPathControlPoint> tempList = new List <FishPathControlPoint>(mControlPoints); tempList.Add(fpcp); mControlPoints = tempList.ToArray(); this.CaculateFinePoints(); }
public void AddPoint(int index) { List <FishPathControlPoint> tempList = new List <FishPathControlPoint>(mControlPoints); FishPathControlPoint newpoint = ScriptableObject.CreateInstance <FishPathControlPoint>(); tempList.Insert(index, newpoint); mControlPoints = tempList.ToArray(); this.CaculateFinePoints(); }
void updateFishPositionAndRotation(int aiStep, float dt) { float lastSpeed = this.mBaseSpeed; float angleFactor = 0; float speedFactor = 0; if (this.mFishPath == null) { return; } int airecordcnt = this.mFishPath.numberOfControlPoints; if (aiStep >= 0 && aiStep < airecordcnt) { FishPathControlPoint curRecord = this.mFishPath.controlPoints[aiStep]; if (curRecord == null) { return; } FishPathControlPoint lastRecord = null; if (aiStep > 0) { lastRecord = this.mFishPath.controlPoints[aiStep - 1]; } //根据玩家炮台位置转换坐标 if (this.mIsNeedRoolUI_) { angleFactor = -(float)curRecord.rotateFactor; } else { angleFactor = (float)curRecord.rotateFactor; } this.mCurSpeed = (float)curRecord.speedFactor * this.mBaseSpeed; this.mCurRotation = this.mCurRotation + angleFactor * dt; } mPosX = mPosX + mCurSpeed * dt * Mathf.Cos(this.mCurRotation); mPosY = mPosY + mCurSpeed * dt * Mathf.Sin(-this.mCurRotation); this.transform.localPosition = new Vector3(mPosX, mPosY, 0); Vector3 eulerAngles = this.transform.eulerAngles; this.transform.eulerAngles = new Vector3(eulerAngles.x, eulerAngles.y, -this.mCurRotation * 57.29577951f); //print ("wzw " + aiStep + " " + angleFactor); }
private void CaculateSpeedAndRotateFactor() { FishPathControlPoint lastPoint = null; for (int i = 0; i < mControlPoints.Length; i++) { if (lastPoint == null) { mControlPoints[i].rotateFactor = Mathf.Deg2Rad * mControlPoints[i].mRotationChange / 0.5f; mControlPoints[i].speedFactor = 1 + (mControlPoints[i].mSpeedScale - 1) / 0.5f * SECOND_ONE_FRAME; } else { mControlPoints[i].rotateFactor = Mathf.Deg2Rad * mControlPoints[i].mRotationChange / 0.5f; mControlPoints[i].speedFactor = lastPoint.mSpeedScale + (mControlPoints[i].mSpeedScale - lastPoint.mSpeedScale) / 0.5f * SECOND_ONE_FRAME; } lastPoint = mControlPoints[i]; } }
public bool load(string filepath) { if (filepath == null || filepath.Length == 0) { Debug.Log("路径文件名不正确"); return(false); } if (File.Exists(filepath) == false) { Debug.Log("路径文件不存在"); return(false); } FileStream fs = new FileStream(filepath, FileMode.Open); StreamReader sr = new StreamReader(fs); FishPath currentPath = null; while (sr.Peek() >= 0) { string oneline = sr.ReadLine(); //Debug.Log(oneline); if (oneline[0] == '#') { continue; } if (oneline[0] == '0') //新的路径 { currentPath = ScriptableObject.CreateInstance <FishPath>(); pathList.Add(currentPath); } string[] cells = oneline.Split('\t'); FishPathControlPoint keyPoint = ScriptableObject.CreateInstance <FishPathControlPoint>(); //keyPoint.id = int.Parse(cells[0]); keyPoint.mRotationChange = float.Parse(cells[1]); keyPoint.mSpeedScale = float.Parse(cells[2]); keyPoint.mTime = 0.5f; currentPath.AddPoint(keyPoint); } return(true); }
public FishPath Load(string filepath) { FishPath fishPath = ScriptableObject.CreateInstance <FishPath>(); FileStream fs = new FileStream(filepath, FileMode.Open); StreamReader sr = new StreamReader(fs); string jsonStr = sr.ReadToEnd(); JsonPath jsonPath = new JsonPath(); jsonPath = JsonMapper.ToObject <JsonPath>(jsonStr); fishPath.ResetPath(); foreach (JsonControlPoint point in jsonPath.pointList) { FishPathControlPoint fpcp = ScriptableObject.CreateInstance <FishPathControlPoint>(); fpcp.mSpeedScale = (float)point.speedScale; fpcp.mRotationChange = (float)point.r; fpcp.mTime = (float)point.time; fishPath.AddPoint(fpcp); } return(fishPath); }
void OnGUI() { GameObject activeGameObject = Selection.activeGameObject; if (null == activeGameObject) { return; } PathRender pathRender = activeGameObject.GetComponent <PathRender>(); if (null == pathRender) { return; } FishPath fishPath = pathRender.FishPathData; if (null == fishPath) { if (GUILayout.Button("创建新路径", GUILayout.Width(100))) { pathRender.FishPathData = new FishPath(); fishPath = pathRender.FishPathData; } return; } NGUIEditorTools.DrawHeader("Control Points", true); { GUILayout.BeginHorizontal(); GUILayout.Space(3f); GUILayout.BeginVertical(); EditorGUILayout.BeginHorizontal(); fishPath.renderPath = EditorGUILayout.Toggle("Render Path", fishPath.renderPath); EditorGUILayout.EndHorizontal(); GUILayout.Space(10); fishPath.lineColor = EditorGUILayout.ColorField("Line Color", fishPath.lineColor); GUILayout.Space(5); EditorGUILayout.BeginHorizontal(); fishPath.baseSpeed = EditorGUILayout.FloatField("Speed", fishPath.baseSpeed); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); fishPath.mPathId = EditorGUILayout.IntField("PathId", fishPath.mPathId); EditorGUILayout.EndHorizontal(); GUILayout.Space(5); if (GUILayout.Button("Load")) { string filepath = EditorUtility.OpenFilePanel("Load", Application.dataPath + "/Resources/Pathes/", "bytes"); if (filepath.Length > 0) { fishPath = AiPathManager.getInstance().loadOnePath(filepath); pathRender.FishPathData = fishPath; EditorUtility.SetDirty(Selection.activeGameObject); } } GUILayout.Space(5); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Save INI")) { AiPathManager.getInstance().saveIni(fishPath); } GUILayout.Space(5); if (GUILayout.Button("Save TAB")) { AiPathManager.getInstance().saveTab(fishPath); } EditorGUILayout.EndHorizontal(); GUILayout.Space(5); if (GUILayout.Button("Reset Path")) { if (EditorUtility.DisplayDialog("Resetting path?", "Are you sure you want to delete all control points?", "Delete", "Cancel")) { fishPath.ResetPath(); EditorUtility.SetDirty(Selection.activeGameObject); return; } } if (selectIndex > -1 && selectIndex < fishPath.numberOfControlPoints) { FishPathControlPoint point = fishPath.controlPoints[selectIndex]; if (point) { point.highLight = GUILayout.Toggle(point.highLight, "HighLight"); point.color = EditorGUILayout.ColorField("Line Colour", point.color); point.mTime = EditorGUILayout.FloatField("Time", point.mTime); point.mSpeedScale = EditorGUILayout.FloatField("SpeedScale", point.mSpeedScale); point.mRotationChange = EditorGUILayout.FloatField("RotationChange", point.mRotationChange); } } EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("添加控制点")) { fishPath.AddPoint(); EditorUtility.SetDirty(pathRender); } EditorGUILayout.EndHorizontal(); mScroll = GUILayout.BeginScrollView(mScroll); bool delete = false; for (int i = 0; i < fishPath.numberOfControlPoints; i++) { GUI.backgroundColor = selectIndex == i ? Color.white : new Color(0.8f, 0.8f, 0.8f); GUILayout.BeginHorizontal("AS TextArea", GUILayout.MinHeight(20f)); GUILayout.Space(5f); GUILayout.Label(i.ToString(), GUILayout.Width(24f)); if (GUILayout.Button("hello", "OL TextField", GUILayout.Height(25f))) { selectIndex = i; fishPath.SelectedLineIndex = selectIndex; } if (GUILayout.Button("X", GUILayout.Width(22f))) { fishPath.DeletePoint(i); if (selectIndex == i) { selectIndex = -1; } i--; } GUILayout.EndHorizontal(); } GUILayout.EndScrollView(); GUILayout.EndVertical(); GUILayout.Space(3f); GUILayout.EndHorizontal(); } if (GUI.changed) { fishPath.CaculateFinePoints(); EditorUtility.SetDirty(Selection.activeGameObject); EditorApplication.MarkSceneDirty(); } }
public override void OnInspectorGUI() { Fish fish = (Fish)target; //if (fish.FishPathData == null) //{ // fish.FishPathData = ScriptableObject.CreateInstance<FishPath>(); // fish.FishPathData.isNewPath = true; // EditorUtility.SetDirty(fish); //} if (fish.FishPathData != null) { EditorGUILayout.BeginHorizontal(); fish.FishPathData.renderPath = EditorGUILayout.Toggle("Render Path", fish.FishPathData.renderPath); EditorGUILayout.EndHorizontal(); if (fish.FishPathData.renderPath) { GUILayout.Space(10); fish.FishPathData.lineColour = EditorGUILayout.ColorField("Line Color", fish.FishPathData.lineColour); GUILayout.Space(5); } EditorGUILayout.BeginHorizontal(); fish.Speed = EditorGUILayout.FloatField("Speed", fish.Speed); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); fish.FishPathData.mPathId = EditorGUILayout.IntField("PathId", fish.FishPathData.mPathId); EditorGUILayout.EndHorizontal(); } GUILayout.Space(5); if (GUILayout.Button("Load")) { string filepath = EditorUtility.OpenFilePanel("Load", Application.dataPath + "/Resources/Pathes/", "bytes"); if (filepath.Length > 0) { fish.FishPathData = PathConfigManager.GetInstance().Load(filepath); fish.FishPathData.FileName = Path.GetFileName(filepath); EditorUtility.SetDirty(fish); } } GUILayout.Space(5); if (GUILayout.Button("Save")) { string savepath = EditorUtility.SaveFilePanel("Save", Application.dataPath + "/Resources/Pathes/", fish.FishPathData.FileName, "bytes"); if (savepath.Length > 0) { PathConfigManager.GetInstance().Save(savepath, fish.FishPathData); AssetDatabase.Refresh(); fish.FishPathData.FileName = Path.GetFileName(savepath); } } if (fish.FishPathData != null) { int numberOfControlPoints = fish.FishPathData.numberOfControlPoints; if (numberOfControlPoints > 0) { GUILayout.Space(5); if (GUILayout.Button("Reset Path")) { if (EditorUtility.DisplayDialog("Resetting path?", "Are you sure you want to delete all control points?", "Delete", "Cancel")) { fish.FishPathData.ResetPath(); fish.FishPathData = null; EditorUtility.SetDirty(fish); return; } } GUILayout.Space(10); GUILayout.Box(EditorGUIUtility.whiteTexture, GUILayout.Height(2), GUILayout.Width(Screen.width - 20)); GUILayout.Space(3); // //scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); for (int i = 0; i < numberOfControlPoints; i++) { FishPathControlPoint point = fish.FishPathData.controlPoints[i]; point.highLight = GUILayout.Toggle(point.highLight, "HighLight"); point.color = EditorGUILayout.ColorField("Line Colour", point.color); point.mTime = EditorGUILayout.FloatField("Time", point.mTime); point.mSpeedScale = EditorGUILayout.FloatField("SpeedScale", point.mSpeedScale); point.mRotationChange = EditorGUILayout.FloatField("RotationChange", point.mRotationChange); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Delete")) { fish.FishPathData.DeletePoint(i); numberOfControlPoints = fish.FishPathData.numberOfControlPoints; if (numberOfControlPoints == 0) { fish.FishPathData = null; } EditorUtility.SetDirty(fish); return; } if (GUILayout.Button("Add New Point After")) { fish.FishPathData.AddPoint(i + 1); EditorUtility.SetDirty(fish); } EditorGUILayout.EndHorizontal(); GUILayout.Space(7); GUILayout.Box(EditorGUIUtility.whiteTexture, GUILayout.Height(2), GUILayout.Width(Screen.width - 25)); GUILayout.Space(7); } // //EditorGUILayout.EndScrollView(); } } else { GUILayout.Space(5); if (GUILayout.Button("Add New Path")) { //Undo.RegisterSceneUndo("Create a new Camera Path point"); fish.FishPathData = ScriptableObject.CreateInstance <FishPath>(); fish.FishPathData.AddPoint(); EditorUtility.SetDirty(fish); } } if (GUI.changed) { if (fish.FishPathData) { fish.FishPathData.CaculateFinePoints(); } EditorUtility.SetDirty(fish); EditorApplication.MarkSceneDirty(); } }