@scenoco-three/unity 0.9.0 → 0.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/convert.d.ts +7 -0
- package/package.json +2 -2
- package/unity/SceNoCoImporter.cs +167 -25
package/dist/convert.d.ts
CHANGED
|
@@ -31,6 +31,13 @@ export type UnityValue = null | number | boolean | string | {
|
|
|
31
31
|
node: string;
|
|
32
32
|
} | {
|
|
33
33
|
asset: string;
|
|
34
|
+
}
|
|
35
|
+
/** A plain settings object — a physics material — which the importer reads as it is. */
|
|
36
|
+
| {
|
|
37
|
+
object: {
|
|
38
|
+
type: string;
|
|
39
|
+
fields: Record<string, number | boolean | string>;
|
|
40
|
+
};
|
|
34
41
|
};
|
|
35
42
|
export interface UnityComponent {
|
|
36
43
|
type: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scenoco-three/unity",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.5",
|
|
4
4
|
"description": "Export a SceNoCo game's prefabs, scenes, meshes and materials into a Unity project: nested prefabs, shared materials, behaviours as MonoBehaviours with their fields wired.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"three",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"playwright-core": "^1.62.1"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
|
-
"@scenoco-three/core": "^0.9.
|
|
56
|
+
"@scenoco-three/core": "^0.9.5",
|
|
57
57
|
"vite": ">=6"
|
|
58
58
|
}
|
|
59
59
|
}
|
package/unity/SceNoCoImporter.cs
CHANGED
|
@@ -374,6 +374,7 @@ namespace SceNoCo
|
|
|
374
374
|
}
|
|
375
375
|
so.ApplyModifiedPropertiesWithoutUndo();
|
|
376
376
|
}
|
|
377
|
+
Physics.WireJoints(n, go, root, warnings.Add);
|
|
377
378
|
if (n.ContainsKey("prefab")) return; // a nested prefab is wired in its own asset
|
|
378
379
|
var children = List(n["children"]).Select(Obj).ToList();
|
|
379
380
|
for (var i = 0; i < children.Count && i < go.transform.childCount; i++) Wire(children[i], go.transform.GetChild(i).gameObject, root);
|
|
@@ -532,20 +533,23 @@ namespace SceNoCo
|
|
|
532
533
|
}
|
|
533
534
|
case "Rigidbody2D":
|
|
534
535
|
{
|
|
536
|
+
// SceNoCo's enums carry Unity's own values.
|
|
535
537
|
var rb = go.AddComponent<Rigidbody2D>();
|
|
536
|
-
|
|
537
|
-
rb.
|
|
538
|
-
rb.
|
|
539
|
-
rb.angularDamping = Num(f, "angularDamping", 0);
|
|
540
|
-
rb.freezeRotation = Bool(f, "fixedRotation", false);
|
|
538
|
+
rb.bodyType = (RigidbodyType2D)(int)Num(f, "bodyType", 0);
|
|
539
|
+
rb.useAutoMass = Bool(f, "useAutoMass", false);
|
|
540
|
+
if (!rb.useAutoMass) rb.mass = Num(f, "mass", 1);
|
|
541
541
|
rb.gravityScale = Num(f, "gravityScale", 1);
|
|
542
|
-
|
|
542
|
+
rb.linearDamping = Num(f, "linearDamping", 0);
|
|
543
|
+
rb.angularDamping = Num(f, "angularDamping", 0.05f);
|
|
544
|
+
rb.constraints = (RigidbodyConstraints2D)(int)Num(f, "constraints", 0);
|
|
545
|
+
if (Bool(f, "freezeRotation", false)) rb.freezeRotation = true;
|
|
546
|
+
rb.collisionDetectionMode = (CollisionDetectionMode2D)(int)Num(f, "collisionDetectionMode", 0);
|
|
543
547
|
return true;
|
|
544
548
|
}
|
|
545
549
|
case "BoxCollider2D":
|
|
546
550
|
{
|
|
547
551
|
var c = go.AddComponent<BoxCollider2D>();
|
|
548
|
-
if (f.ContainsKey("
|
|
552
|
+
if (f.ContainsKey("size")) c.size = V2(f["size"]);
|
|
549
553
|
SetCollider2D(c, f);
|
|
550
554
|
return true;
|
|
551
555
|
}
|
|
@@ -559,8 +563,8 @@ namespace SceNoCo
|
|
|
559
563
|
case "CapsuleCollider2D":
|
|
560
564
|
{
|
|
561
565
|
var c = go.AddComponent<CapsuleCollider2D>();
|
|
562
|
-
if (f.ContainsKey("size"))
|
|
563
|
-
c.direction =
|
|
566
|
+
if (f.ContainsKey("size")) c.size = V2(f["size"]);
|
|
567
|
+
c.direction = (CapsuleDirection2D)(int)Num(f, "direction", 0);
|
|
564
568
|
SetCollider2D(c, f);
|
|
565
569
|
return true;
|
|
566
570
|
}
|
|
@@ -572,28 +576,154 @@ namespace SceNoCo
|
|
|
572
576
|
return true;
|
|
573
577
|
}
|
|
574
578
|
case "EdgeCollider2D":
|
|
575
|
-
case "ChainCollider2D":
|
|
576
579
|
{
|
|
577
580
|
var c = go.AddComponent<EdgeCollider2D>();
|
|
578
|
-
if (
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
581
|
+
if (f.ContainsKey("points")) c.points = Points(f["points"]);
|
|
582
|
+
SetCollider2D(c, f);
|
|
583
|
+
return true;
|
|
584
|
+
}
|
|
585
|
+
// Joints: `connectedBody` is another node's, set by `WireJoints` once every node is built.
|
|
586
|
+
case "FixedJoint2D":
|
|
587
|
+
{
|
|
588
|
+
var j = go.AddComponent<FixedJoint2D>();
|
|
589
|
+
SetAnchored(j, f, true);
|
|
590
|
+
j.frequency = Num(f, "frequency", 0);
|
|
591
|
+
j.dampingRatio = Num(f, "dampingRatio", 0);
|
|
592
|
+
return true;
|
|
593
|
+
}
|
|
594
|
+
case "HingeJoint2D":
|
|
595
|
+
{
|
|
596
|
+
var j = go.AddComponent<HingeJoint2D>();
|
|
597
|
+
SetAnchored(j, f, true);
|
|
598
|
+
j.useLimits = Bool(f, "useLimits", false);
|
|
599
|
+
if (f.ContainsKey("limits")) { var l = Fields(f["limits"]); j.limits = new JointAngleLimits2D { min = Num(l, "min", 0), max = Num(l, "max", 359) }; }
|
|
600
|
+
j.useMotor = Bool(f, "useMotor", false);
|
|
601
|
+
if (f.ContainsKey("motor")) j.motor = Motor(f["motor"]);
|
|
602
|
+
return true;
|
|
603
|
+
}
|
|
604
|
+
case "DistanceJoint2D":
|
|
605
|
+
{
|
|
606
|
+
var j = go.AddComponent<DistanceJoint2D>();
|
|
607
|
+
SetAnchored(j, f, false);
|
|
608
|
+
j.autoConfigureDistance = Bool(f, "autoConfigureDistance", true);
|
|
609
|
+
if (f.ContainsKey("distance")) j.distance = Num(f, "distance", 1);
|
|
610
|
+
j.maxDistanceOnly = Bool(f, "maxDistanceOnly", false);
|
|
611
|
+
return true;
|
|
612
|
+
}
|
|
613
|
+
case "SpringJoint2D":
|
|
614
|
+
{
|
|
615
|
+
var j = go.AddComponent<SpringJoint2D>();
|
|
616
|
+
SetAnchored(j, f, false);
|
|
617
|
+
j.autoConfigureDistance = Bool(f, "autoConfigureDistance", true);
|
|
618
|
+
if (f.ContainsKey("distance")) j.distance = Num(f, "distance", 1);
|
|
619
|
+
j.dampingRatio = Num(f, "dampingRatio", 0);
|
|
620
|
+
j.frequency = Num(f, "frequency", 1);
|
|
621
|
+
return true;
|
|
622
|
+
}
|
|
623
|
+
case "SliderJoint2D":
|
|
624
|
+
{
|
|
625
|
+
var j = go.AddComponent<SliderJoint2D>();
|
|
626
|
+
SetAnchored(j, f, true);
|
|
627
|
+
j.autoConfigureAngle = Bool(f, "autoConfigureAngle", false);
|
|
628
|
+
j.angle = Num(f, "angle", 0);
|
|
629
|
+
j.useLimits = Bool(f, "useLimits", false);
|
|
630
|
+
if (f.ContainsKey("limits")) { var l = Fields(f["limits"]); j.limits = new JointTranslationLimits2D { min = Num(l, "min", 0), max = Num(l, "max", 0) }; }
|
|
631
|
+
j.useMotor = Bool(f, "useMotor", false);
|
|
632
|
+
if (f.ContainsKey("motor")) j.motor = Motor(f["motor"]);
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
case "WheelJoint2D":
|
|
636
|
+
{
|
|
637
|
+
var j = go.AddComponent<WheelJoint2D>();
|
|
638
|
+
SetAnchored(j, f, true);
|
|
639
|
+
if (f.ContainsKey("suspension"))
|
|
585
640
|
{
|
|
586
|
-
var
|
|
587
|
-
|
|
588
|
-
c.points = pts.ToArray();
|
|
641
|
+
var s = Fields(f["suspension"]);
|
|
642
|
+
j.suspension = new JointSuspension2D { dampingRatio = Num(s, "dampingRatio", 0.7f), frequency = Num(s, "frequency", 2), angle = Num(s, "angle", 90) };
|
|
589
643
|
}
|
|
590
|
-
|
|
644
|
+
j.useMotor = Bool(f, "useMotor", false);
|
|
645
|
+
if (f.ContainsKey("motor")) j.motor = Motor(f["motor"]);
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
648
|
+
case "FrictionJoint2D":
|
|
649
|
+
{
|
|
650
|
+
var j = go.AddComponent<FrictionJoint2D>();
|
|
651
|
+
SetAnchored(j, f, true);
|
|
652
|
+
j.maxForce = Num(f, "maxForce", 1);
|
|
653
|
+
j.maxTorque = Num(f, "maxTorque", 1);
|
|
654
|
+
return true;
|
|
655
|
+
}
|
|
656
|
+
case "RelativeJoint2D":
|
|
657
|
+
{
|
|
658
|
+
var j = go.AddComponent<RelativeJoint2D>();
|
|
659
|
+
SetJoint(j, f);
|
|
660
|
+
j.maxForce = Num(f, "maxForce", 10000);
|
|
661
|
+
j.maxTorque = Num(f, "maxTorque", 10000);
|
|
662
|
+
j.correctionScale = Num(f, "correctionScale", 0.3f);
|
|
663
|
+
j.autoConfigureOffset = Bool(f, "autoConfigureOffset", true);
|
|
664
|
+
if (f.ContainsKey("linearOffset")) j.linearOffset = V2(f["linearOffset"]);
|
|
665
|
+
if (f.ContainsKey("angularOffset")) j.angularOffset = Num(f, "angularOffset", 0);
|
|
666
|
+
return true;
|
|
667
|
+
}
|
|
668
|
+
case "TargetJoint2D":
|
|
669
|
+
{
|
|
670
|
+
var j = go.AddComponent<TargetJoint2D>();
|
|
671
|
+
SetJoint(j, f);
|
|
672
|
+
if (f.ContainsKey("anchor")) j.anchor = V2(f["anchor"]);
|
|
673
|
+
j.autoConfigureTarget = Bool(f, "autoConfigureTarget", true);
|
|
674
|
+
if (f.ContainsKey("target")) j.target = V2(f["target"]);
|
|
675
|
+
j.maxForce = Num(f, "maxForce", 1000);
|
|
676
|
+
j.dampingRatio = Num(f, "dampingRatio", 1);
|
|
677
|
+
j.frequency = Num(f, "frequency", 5);
|
|
591
678
|
return true;
|
|
592
679
|
}
|
|
593
680
|
}
|
|
594
681
|
return false;
|
|
595
682
|
}
|
|
596
683
|
|
|
684
|
+
static void SetJoint(Joint2D j, Dictionary<string, object> f)
|
|
685
|
+
{
|
|
686
|
+
j.enableCollision = Bool(f, "enableCollision", false);
|
|
687
|
+
if (f.ContainsKey("breakForce")) j.breakForce = Num(f, "breakForce", float.PositiveInfinity);
|
|
688
|
+
if (f.ContainsKey("breakTorque")) j.breakTorque = Num(f, "breakTorque", float.PositiveInfinity);
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/** `autoConfigureConnectedAnchor` before `connectedAnchor`: Unity overwrites the anchor while it is on. */
|
|
692
|
+
static void SetAnchored(AnchoredJoint2D j, Dictionary<string, object> f, bool autoByDefault)
|
|
693
|
+
{
|
|
694
|
+
SetJoint(j, f);
|
|
695
|
+
if (f.ContainsKey("anchor")) j.anchor = V2(f["anchor"]);
|
|
696
|
+
j.autoConfigureConnectedAnchor = Bool(f, "autoConfigureConnectedAnchor", autoByDefault);
|
|
697
|
+
if (f.ContainsKey("connectedAnchor")) j.connectedAnchor = V2(f["connectedAnchor"]);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/** A settings object's fields: `{ object: { fields } }`. */
|
|
701
|
+
static Dictionary<string, object> Fields(object v) => Obj(Obj(Obj(v)["object"])["fields"]);
|
|
702
|
+
|
|
703
|
+
static JointMotor2D Motor(object v)
|
|
704
|
+
{
|
|
705
|
+
var m = Fields(v);
|
|
706
|
+
return new JointMotor2D { motorSpeed = Num(m, "motorSpeed", 0), maxMotorTorque = Num(m, "maxMotorTorque", 10000) };
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
/** Each joint's `connectedBody`: the Rigidbody2D on the node it names, joints matched by type in order. */
|
|
710
|
+
public static void WireJoints(Dictionary<string, object> n, GameObject go, GameObject root, Action<string> warn)
|
|
711
|
+
{
|
|
712
|
+
var seen = new Dictionary<string, int>();
|
|
713
|
+
foreach (var comp in List(n["components"]).Select(Obj).Where(c => !(bool)c["game"]))
|
|
714
|
+
{
|
|
715
|
+
var type = Str(comp, "type");
|
|
716
|
+
var t = typeof(Joint2D).Assembly.GetType("UnityEngine." + type);
|
|
717
|
+
if (t == null || !typeof(Joint2D).IsAssignableFrom(t)) continue;
|
|
718
|
+
seen[type] = seen.TryGetValue(type, out var i) ? i + 1 : 0;
|
|
719
|
+
if (!Obj(comp["fields"]).TryGetValue("connectedBody", out var link) || !(link is Dictionary<string, object> l) || !l.TryGetValue("node", out var path)) continue;
|
|
720
|
+
var joint = (Joint2D)go.GetComponents(t)[seen[type]];
|
|
721
|
+
var target = Find(root, (string)path);
|
|
722
|
+
joint.connectedBody = target != null ? target.GetComponent<Rigidbody2D>() : null;
|
|
723
|
+
if (joint.connectedBody == null) warn($"{type} on {go.name}: its connectedBody was not found");
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
597
727
|
static void SetCollider3D(Collider c, Dictionary<string, object> f)
|
|
598
728
|
{
|
|
599
729
|
c.isTrigger = Bool(f, "isTrigger", false);
|
|
@@ -613,10 +743,16 @@ namespace SceNoCo
|
|
|
613
743
|
|
|
614
744
|
static void SetCollider2D(Collider2D c, Dictionary<string, object> f)
|
|
615
745
|
{
|
|
616
|
-
c.isTrigger = Bool(f, "
|
|
617
|
-
if (
|
|
618
|
-
|
|
619
|
-
|
|
746
|
+
c.isTrigger = Bool(f, "isTrigger", false);
|
|
747
|
+
if (f.ContainsKey("offset")) c.offset = V2(f["offset"]);
|
|
748
|
+
if (f.ContainsKey("density")) c.density = Num(f, "density", 1);
|
|
749
|
+
// A shared PhysicsMaterial2D, or the collider's own friction and bounciness: one asset per pair.
|
|
750
|
+
var surface = f.TryGetValue("material", out var mv) && mv is Dictionary<string, object> mo && mo.TryGetValue("object", out var ov)
|
|
751
|
+
? Obj(Obj(ov)["fields"])
|
|
752
|
+
: f;
|
|
753
|
+
if (!surface.ContainsKey("friction") && !surface.ContainsKey("bounciness")) return;
|
|
754
|
+
var friction = Num(surface, "friction", 0.4f);
|
|
755
|
+
var bounce = Num(surface, "bounciness", 0);
|
|
620
756
|
var path = $"Assets/Materials/Physics/Friction{friction:0.##}_Bounce{bounce:0.##}_2D.asset".Replace(',', '.');
|
|
621
757
|
var m = AssetDatabase.LoadAssetAtPath<PhysicsMaterial2D>(path);
|
|
622
758
|
if (m == null)
|
|
@@ -628,6 +764,12 @@ namespace SceNoCo
|
|
|
628
764
|
c.sharedMaterial = m;
|
|
629
765
|
}
|
|
630
766
|
|
|
767
|
+
static Vector2 V2(object v)
|
|
768
|
+
{
|
|
769
|
+
var p = Floats(Obj(v)["v2"]);
|
|
770
|
+
return new Vector2(p[0], p[1]);
|
|
771
|
+
}
|
|
772
|
+
|
|
631
773
|
static Vector2[] Points(object v) =>
|
|
632
774
|
List(Obj(v)["array"]).Select(p => Floats(Obj(p)["v2"])).Select(p => new Vector2(p[0], p[1])).ToArray();
|
|
633
775
|
}
|