示例#1
0
        protected override bool Initialize()
        {
            WireRoute.ValidatedRoute validatedRoute = Route.GetValidated();
            if (!validatedRoute.Valid)
            {
                Debug.LogError(validatedRoute.ErrorString, this);
                for (int i = 0; i < validatedRoute.Nodes.Count; ++i)
                {
                    if (!validatedRoute.Nodes[i].Valid)
                    {
                        Debug.LogError("[" + i + "]: " + validatedRoute.Nodes[i].ErrorString, this);
                    }
                }

                return(false);
            }

            try {
                Native   = new agxWire.Wire(Radius, ResolutionPerUnitLength);
                Material = m_material != null?m_material.GetInitialized <ShapeMaterial>() : null;

                int nodeCounter = 0;
                foreach (WireRouteNode routeNode in Route)
                {
                    agxWire.WireNode node = routeNode.GetInitialized <WireRouteNode>().Native;

                    bool success = true;
                    if (node.getType() == agxWire.WireNode.Type.CONNECTING)
                    {
                        // This is the first node, CM-node goes first.
                        if (nodeCounter == 0)
                        {
                            success = success && Native.add(node.getAsConnecting().getCmNode());
                            success = success && Native.add(node);
                        }
                        // This has to be the last node, CM-node goes last.
                        else
                        {
                            success = success && Native.add(node);
                            success = success && Native.add(node.getAsConnecting().getCmNode());
                        }
                    }
                    else if (routeNode.Type == NodeType.WinchNode)
                    {
                        if (node == null)
                        {
                            throw new AGXUnity.Exception("Unable to initialize wire winch.");
                        }

                        success = success && Native.add(routeNode.Winch.Native);
                    }
                    else
                    {
                        success = success && Native.add(node);
                    }

                    if (!success)
                    {
                        throw new AGXUnity.Exception("Unable to add node " + nodeCounter + ": " + routeNode.Type);
                    }

                    ++nodeCounter;
                }

                Native.setName(name);

                GetSimulation().add(Native);
                Simulation.Instance.StepCallbacks.PostStepForward += OnPostStepForward;
            }
            catch (System.Exception e) {
                Debug.LogException(e, this);
                return(false);
            }

            return(Native.initialized());
        }