private static bool SetUpProceduralTool(ref ToolController toolController, ref List <ToolBase> extraTools) { ProceduralTool proceduralTool = toolController.gameObject.GetComponent <ProceduralTool>(); if (proceduralTool == null) { proceduralTool = toolController.gameObject.AddComponent <ProceduralTool>(); extraTools.Add(proceduralTool); // Debug.Log("[ProceduralObjects] ToolMan.SetupProceduralTool(): Added ProceduralTool to toolController. Returning true..."); return(true); } else { Debug.Log("[ProceduralObjects] ToolMan.SetupProceduralTool(): ProceduralTool already exists in the toolController. Returning false..."); return(false); } }
protected override void OnToolUpdate() { base.OnToolUpdate(); switch (ProceduralObjectsLogic.toolAction) { case ToolAction.none: switch (ProceduralObjectsLogic.axisState) { case AxisEditionState.none: ToolCursor = (CursorInfo)null; break; case AxisEditionState.X: case AxisEditionState.Z: ToolCursor = terrainLevel; break; case AxisEditionState.Y: ToolCursor = terrainShift; break; } base.ShowToolInfo(false, "", Vector3.zero); break; case ToolAction.vertices: string toolInfo = ""; switch (ProceduralObjectsLogic.verticesToolType) { case 0: ToolCursor = moveVertices; toolInfo = LocalizationManager.instance.current["position"]; break; case 1: ToolCursor = rotateVertices; toolInfo = LocalizationManager.instance.current["rotation"]; break; case 2: ToolCursor = scaleVertices; toolInfo = LocalizationManager.instance.current["scale_obj"]; break; } if (ProceduralObjectsLogic.tabSwitchTimer != 0f) { Vector3 pos = Vector3.zero; ToolBase.RaycastInput rayInput = new ToolBase.RaycastInput(Camera.main.ScreenPointToRay(Input.mousePosition), Camera.main.farClipPlane); ToolBase.RaycastOutput rayOutput; if (ProceduralTool.TerrainRaycast(rayInput, out rayOutput)) { pos = rayOutput.m_hitPos; } base.ShowToolInfo(true, toolInfo, pos); } else { base.ShowToolInfo(false, "", Vector3.zero); } break; case ToolAction.build: ToolCursor = buildCursor; base.ShowToolInfo(true, LocalizationManager.instance.current["click_to_place"], ProceduralObjectsLogic.movingWholeRaycast); break; } }
//Last Updated 160622 private static bool AddExtraToolsToController(ref ToolController toolController, List <ToolBase> extraTools) { bool _result = false; Debug.Log("[ProceduralObjects] Begin ToolMan.AddExtraToolsToController()."); Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Progress: 0/4 [ ]"); if (toolController == null) { Debug.LogError("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Failed to append ProceduralTool to tool controllers: toolController parameter is null."); return(false); } if (extraTools == null) { Debug.LogError("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Failed to append ProceduralTool to tool controllers: extraTools parameter is null."); return(false); } if (extraTools.Count < 1) { Debug.LogWarning("[ProceduralObjects] ToolMan.AddExtraToolsToController(): No tools were found in the extraTools parameter."); return(false); } Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Progress: 1/4 [= ]"); var _toolsFieldInfo = typeof(ToolController).GetField("m_tools", BindingFlags.Instance | BindingFlags.NonPublic); var _tools = (ToolBase[])_toolsFieldInfo.GetValue(toolController); if (_tools == null) { //old //Debug.LogError("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Failed to append ProceduralTool to tool controllers: Could not FieldInfo.GetValue() from ToolController.m_tools."); //return false; //new Debug.LogWarning("[ProceduralObjects] ToolMan.AddExtraToolsToController(): toolController.m_tools was detected to be null from FieldInfo.GetValue()."); Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Attempting to pre-populate toolController.m_tools by calling toolController.GetComponents<ToolBase>()."); _tools = toolController.GetComponents <ToolBase>(); var _tools2 = (ToolBase[])_toolsFieldInfo.GetValue(toolController); if (_tools2 == null) { Debug.LogError("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Failed to pre-populate toolController.m_tools. Returning false..."); return(false); } else if (_tools.Length > 0) { Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): The attempt to pre-populate toolController.m_tools appears to have been successful..."); Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Pre-populated toolController.m_tools with >>" + _tools.Length + "<< tools."); } else { Debug.LogError("[ProceduralObjects] ToolMan.AddExtraToolsToController(): The attempt to pre-populate toolController.m_tools failed. No tools were found. Returning false..."); return(false); } } if (_tools.Length < 1) { Debug.LogWarning("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Initial ToolController.m_tools has a length < 1. Its length is: " + _tools.Length + "."); if (extraTools.Count < 1) { Debug.LogError("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Returning false since both toolController.m_tools and extraTools have a length < 1."); return(false); } } Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Progress: 2/4 [== ]"); //Prepare toolController.m_tools for appending new elements int _initialLength = _tools.Length; Array.Resize <ToolBase>(ref _tools, _initialLength + extraTools.Count); var index = 0; //find ToolsModifierControl tool dictionary var _dictionary = (Dictionary <Type, ToolBase>) typeof(ToolsModifierControl).GetField("m_Tools", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null); if (_dictionary == null) { //old //Debug.LogError("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Failed to append ProceduralTool to tool controllers: Could not find ToolsModifierControl.m_Tools dictionary."); //return false; //new Debug.LogWarning("[ProceduralObjects] ToolMan.AddExtraToolsToController(): ToolsModifierControl.m_Tools dictionary was detected to be null from FieldInfo.GetField()."); Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Attempting to pre-populate ToolsModifierControl.m_Tools dictionary by calling ToolsModifierControl.GetTool<PropTool>()..."); var _propTool = ToolsModifierControl.GetTool <PropTool>(); var _dictionary2 = (Dictionary <Type, ToolBase>) typeof(ToolsModifierControl).GetField("m_Tools", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null); if (_dictionary2 == null) { Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Failed in pre-populating ToolsModifierControl.m_Tools by calling ToolsModifierControl.GetTool<PropTool>()..."); Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Attempting to pre-populate ToolsModifierControl.m_Tools by copying the >>" + _tools.Length + "<< elements of toolController.m_tools."); //Our own version of ToolsModifierControl.CollectTools() _dictionary = new Dictionary <Type, ToolBase>(_initialLength + extraTools.Count); for (int i = 0; i < _tools.Length; i++) { _dictionary.Add(_tools[i].GetType(), _tools[i]); } var _dictionary3 = (Dictionary <Type, ToolBase>) typeof(ToolsModifierControl).GetField("m_Tools", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null); if (_dictionary3 == null) { Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Failed in pre-populating ToolsModifierControl.m_Tools by copying the >>" + _tools.Length + "<< elements of toolController.m_tools."); Debug.LogError("[ProceduralObjects] ToolMan.AddExtraToolsToController(): All attempts to pre-populate ToolsModifierControl.m_Tools failed. ): Returning false..."); return(false); } else { _dictionary = _dictionary3; Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): The attempt to pre-populate ToolsModifierControl.m_Tools dictionary by copying the >>" + _tools.Length + "<< elements of toolController.m_tools appears to have been successful..."); } } else { _dictionary = _dictionary2; Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): The attempt to pre-populate ToolsModifierControl.m_Tools dictionary by calling ToolsModifierControl.GetTool<PropTool>() appears to have been successful..."); } Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Verifying the length of the ToolBase collections for both tool controllers..."); //check that lengths match if (_dictionary == null) { Debug.LogError("[ProceduralObjects] ToolMan.AddExtraToolsToController(): _dictionary is null! Returning false..."); return(false); } if (_dictionary.Count == _tools.Length) { Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Successfully pre-populated ToolsModifierControl.m_Tools dictionary. Its Count is: " + _dictionary2.Count + "."); } else { //actually since we resized _tools earlier, _tools should be extraTools.Length (1) longer than _dictionary. Debug.LogWarning("[ProceduralObjects] ToolMan.AddExtraToolsToController(): ToolsModifierControl.m_Tools.Count does not equal toolController.m_tools.Length"); Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): ToolsModifierControl.m_Tools.Count = " + _dictionary2.Count + ", and toolController.m_tools.Length = " + _tools.Length + "."); } } else if (_dictionary.Count < 1) { Debug.LogWarning("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Initial ToolsModifierControl.m_Tools dictionary has a count < 1. Its count is: " + _dictionary.Count + "."); } //append ProceduralTool to ToolBase collections foreach (var _currentTool in extraTools) { _dictionary.Add(_currentTool.GetType(), _currentTool); _tools[_initialLength + index] = _currentTool; index++; } Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Progress: 3/4 [=== ]"); _toolsFieldInfo.SetValue(toolController, _tools); ProceduralTool proceduralTool = ToolsModifierControl.GetTool <ProceduralTool>(); if (proceduralTool == null) { Debug.LogError("[ProceduralObjects] ToolMan.AddExtraToolsToController(): ProceduralTool was not found in ToolsModifierControl after appending to the tool dictionary."); return(false); } Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Progress: 4/4 [====]"); Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Success in appending ProceduralTool to tool controllers!"); //160815 2306 //These are only really necessary as I suspect two instances of ProceduralTool are being created for some users (NullReferenceException when placing each line). //I no longer believe this ^ to be the case, so we comment these out. //Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): Outputting toolController lists:"); //Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): toolController.m_tools array has " + _tools.Length + " members:"); //for (int i = 0; i < _tools.Length; i++) //{ // Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): toolController.m_tools[" + i + "] = " + _tools[i]); //} //Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): ToolsModifierControl.m_Tools dictionary has " + _dictionary.Count + " members:"); //var _keys = _dictionary.Keys.ToList<Type>(); //foreach (Type _type in _keys) //{ // Debug.Log("[ProceduralObjects] ToolMan.AddExtraToolsToController(): ToolsModifierControl.m_Tools[" + _type + "] = " + _dictionary[_type]); //} _result = true; return(_result); }