AABB
AlignmentBehavior
ArriveBehavior
AStar
BFS
BoundingSphere
BVH
BVHNode
Cell
CellSpacePartitioning
CohesionBehavior
CompositeGoal
ConvexHull
Corridor
CostTable
DFS
Dijkstra
Edge
EntityManager
EvadeBehavior
EventDispatcher
Behavior
FollowPathBehavior
FuzzyAND
FuzzyCompositeTerm
FuzzyFAIRLY
FuzzyModule
FuzzyOR
FuzzyRule
FuzzySet
FuzzyTerm
FuzzyVariable
FuzzyVERY
GameEntity
Goal
GoalEvaluator
Graph
GraphUtils
HalfEdge
HeuristicPolicyDijkstra
HeuristicPolicyEuclid
HeuristicPolicyEuclidSquared
HeuristicPolicyManhattan
InterposeBehavior
LeftSCurveFuzzySet
LeftShoulderFuzzySet
LineSegment
Logger
MathUtils
Matrix3
Matrix4
MemoryRecord
MemorySystem
MeshGeometry
MessageDispatcher
MovingEntity
NavEdge
NavMesh
NavMeshLoader
NavNode
Node
NormalDistFuzzySet
OBB
ObstacleAvoidanceBehavior
OffsetPursuitBehavior
OnPathBehavior
Path
Plane
Polygon
Polyhedron
PriorityQueue
PursuitBehavior
Quaternion
Ray
RectangleTriggerRegion
Regular
RightSCurveFuzzySet
RightShoulderFuzzySet
SAT
SeekBehavior
SeparationBehavior
SingletonFuzzySet
Smoother
SphericalTriggerRegion
State
StateMachine
SteeringBehavior
SteeringManager
Task
TaskQueue
Telegram
Think
Time
TriangularFuzzySet
Trigger
TriggerRegion
Vector3
Vehicle
Version
WanderBehavior

fromJSON

该方法通过从JSON数据中创建成本表对象。

语法

CostTable.fromJSON(data);

参数

  • data:Object,必需。包含成本对象数据的JSON对象。

返回值

返回一个新的CostTable对象。

例子

const data = {
    "name": "My Cost Table",
    "currency": "USD",
    "costs": [
        {
            "name": "Item 1",
            "cost": 10.99,
            "quantity": 2
        },
        {
            "name": "Item 2",
            "cost": 5.99,
            "quantity": 3
        }
    ]
};

const costTable = CostTable.fromJSON(data);

在HTML文档中使用

您可以通过向HTML文档中添加以下代码来使用CostTable fromJSON方法:

<script src="path/to/yuka.js"></script>
<script>
  // 通过fetch获取JSON数据
  fetch('data.json').then(response => {
    return response.json();
  }).then(data => {
    const costTable = CostTable.fromJSON(data);
    console.log(costTable);
  });
</script>

JSON数据格式说明

您需要将JSON数据格式化为包含以下属性的对象:

  • name - 表名称
  • currency - 表示该成本表使用的货币
  • costs - 包含成本条目的数组,每个成本条目具有以下属性:
    • name - 物品名称
    • cost - 单价
    • quantity - 数量

完整的示例:

{
  "name": "My Cost Table",
  "currency": "USD",
  "costs": [
    {
      "name": "Item 1",
      "cost": 10.99,
      "quantity": 2
    },
    {
      "name": "Item 2",
      "cost": 5.99,
      "quantity": 3
    }
  ]
}

注意事项

  • 要使用fromJSON方法创建CostTable对象,必须在引入Yuka库后调用该方法。
  • 数据类型应该尽量匹配成本表对象定义中的类型,否则可能会抛出异常。